Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework\Constraint;
11
 
12
use function array_map;
13
use function array_values;
14
use function count;
15
 
16
/**
17
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
18
 */
19
abstract class BinaryOperator extends Operator
20
{
21
    /**
22
     * @var Constraint[]
23
     */
24
    private $constraints = [];
25
 
26
    public static function fromConstraints(Constraint ...$constraints): self
27
    {
28
        $constraint = new static;
29
 
30
        $constraint->constraints = $constraints;
31
 
32
        return $constraint;
33
    }
34
 
35
    /**
36
     * @param mixed[] $constraints
37
     */
38
    public function setConstraints(array $constraints): void
39
    {
40
        $this->constraints = array_map(function ($constraint): Constraint
41
        {
42
            return $this->checkConstraint($constraint);
43
        }, array_values($constraints));
44
    }
45
 
46
    /**
47
     * Returns the number of operands (constraints).
48
     */
49
    final public function arity(): int
50
    {
51
        return count($this->constraints);
52
    }
53
 
54
    /**
55
     * Returns a string representation of the constraint.
56
     */
57
    public function toString(): string
58
    {
59
        $reduced = $this->reduce();
60
 
61
        if ($reduced !== $this) {
62
            return $reduced->toString();
63
        }
64
 
65
        $text = '';
66
 
67
        foreach ($this->constraints as $key => $constraint) {
68
            $constraint = $constraint->reduce();
69
 
70
            $text .= $this->constraintToString($constraint, $key);
71
        }
72
 
73
        return $text;
74
    }
75
 
76
    /**
77
     * Counts the number of constraint elements.
78
     */
79
    public function count(): int
80
    {
81
        $count = 0;
82
 
83
        foreach ($this->constraints as $constraint) {
84
            $count += count($constraint);
85
        }
86
 
87
        return $count;
88
    }
89
 
90
    /**
91
     * Returns the nested constraints.
92
     */
93
    final protected function constraints(): array
94
    {
95
        return $this->constraints;
96
    }
97
 
98
    /**
99
     * Returns true if the $constraint needs to be wrapped with braces.
100
     */
101
    final protected function constraintNeedsParentheses(Constraint $constraint): bool
102
    {
103
        return $this->arity() > 1 && parent::constraintNeedsParentheses($constraint);
104
    }
105
 
106
    /**
107
     * Reduces the sub-expression starting at $this by skipping degenerate
108
     * sub-expression and returns first descendant constraint that starts
109
     * a non-reducible sub-expression.
110
     *
111
     * See Constraint::reduce() for more.
112
     */
113
    protected function reduce(): Constraint
114
    {
115
        if ($this->arity() === 1 && $this->constraints[0] instanceof Operator) {
116
            return $this->constraints[0]->reduce();
117
        }
118
 
119
        return parent::reduce();
120
    }
121
 
122
    /**
123
     * Returns string representation of given operand in context of this operator.
124
     *
125
     * @param Constraint $constraint operand constraint
126
     * @param int        $position   position of $constraint in this expression
127
     */
128
    private function constraintToString(Constraint $constraint, int $position): string
129
    {
130
        $prefix = '';
131
 
132
        if ($position > 0) {
133
            $prefix = (' ' . $this->operator() . ' ');
134
        }
135
 
136
        if ($this->constraintNeedsParentheses($constraint)) {
137
            return $prefix . '( ' . $constraint->toString() . ' )';
138
        }
139
 
140
        $string = $constraint->toStringInContext($this, $position);
141
 
142
        if ($string === '') {
143
            $string = $constraint->toString();
144
        }
145
 
146
        return $prefix . $string;
147
    }
148
}