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 count;
13
 
14
/**
15
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
16
 */
17
abstract class UnaryOperator extends Operator
18
{
19
    /**
20
     * @var Constraint
21
     */
22
    private $constraint;
23
 
24
    /**
25
     * @param Constraint|mixed $constraint
26
     */
27
    public function __construct($constraint)
28
    {
29
        $this->constraint = $this->checkConstraint($constraint);
30
    }
31
 
32
    /**
33
     * Returns the number of operands (constraints).
34
     */
35
    public function arity(): int
36
    {
37
        return 1;
38
    }
39
 
40
    /**
41
     * Returns a string representation of the constraint.
42
     */
43
    public function toString(): string
44
    {
45
        $reduced = $this->reduce();
46
 
47
        if ($reduced !== $this) {
48
            return $reduced->toString();
49
        }
50
 
51
        $constraint = $this->constraint->reduce();
52
 
53
        if ($this->constraintNeedsParentheses($constraint)) {
54
            return $this->operator() . '( ' . $constraint->toString() . ' )';
55
        }
56
 
57
        $string = $constraint->toStringInContext($this, 0);
58
 
59
        if ($string === '') {
60
            return $this->transformString($constraint->toString());
61
        }
62
 
63
        return $string;
64
    }
65
 
66
    /**
67
     * Counts the number of constraint elements.
68
     */
69
    public function count(): int
70
    {
71
        return count($this->constraint);
72
    }
73
 
74
    /**
75
     * Returns the description of the failure.
76
     *
77
     * The beginning of failure messages is "Failed asserting that" in most
78
     * cases. This method should return the second part of that sentence.
79
     *
80
     * @param mixed $other evaluated value or object
81
     *
82
     * @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
83
     */
84
    protected function failureDescription($other): string
85
    {
86
        $reduced = $this->reduce();
87
 
88
        if ($reduced !== $this) {
89
            return $reduced->failureDescription($other);
90
        }
91
 
92
        $constraint = $this->constraint->reduce();
93
 
94
        if ($this->constraintNeedsParentheses($constraint)) {
95
            return $this->operator() . '( ' . $constraint->failureDescription($other) . ' )';
96
        }
97
 
98
        $string = $constraint->failureDescriptionInContext($this, 0, $other);
99
 
100
        if ($string === '') {
101
            return $this->transformString($constraint->failureDescription($other));
102
        }
103
 
104
        return $string;
105
    }
106
 
107
    /**
108
     * Transforms string returned by the memeber constraint's toString() or
109
     * failureDescription() such that it reflects constraint's participation in
110
     * this expression.
111
     *
112
     * The method may be overwritten in a subclass to apply default
113
     * transformation in case the operand constraint does not provide its own
114
     * custom strings via toStringInContext() or failureDescriptionInContext().
115
     *
116
     * @param string $string the string to be transformed
117
     */
118
    protected function transformString(string $string): string
119
    {
120
        return $string;
121
    }
122
 
123
    /**
124
     * Provides access to $this->constraint for subclasses.
125
     */
126
    final protected function constraint(): Constraint
127
    {
128
        return $this->constraint;
129
    }
130
 
131
    /**
132
     * Returns true if the $constraint needs to be wrapped with parentheses.
133
     */
134
    protected function constraintNeedsParentheses(Constraint $constraint): bool
135
    {
136
        $constraint = $constraint->reduce();
137
 
138
        return $constraint instanceof self || parent::constraintNeedsParentheses($constraint);
139
    }
140
}