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 count;
14
use function preg_match;
15
use function preg_quote;
16
use function preg_replace;
17
 
18
/**
19
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
20
 */
21
final class LogicalNot extends UnaryOperator
22
{
23
    public static function negate(string $string): string
24
    {
25
        $positives = [
26
            'contains ',
27
            'exists',
28
            'has ',
29
            'is ',
30
            'are ',
31
            'matches ',
32
            'starts with ',
33
            'ends with ',
34
            'reference ',
35
            'not not ',
36
        ];
37
 
38
        $negatives = [
39
            'does not contain ',
40
            'does not exist',
41
            'does not have ',
42
            'is not ',
43
            'are not ',
44
            'does not match ',
45
            'starts not with ',
46
            'ends not with ',
47
            'don\'t reference ',
48
            'not ',
49
        ];
50
 
51
        preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches);
52
 
53
        $positives = array_map(static function (string $s)
54
        {
55
            return '/\\b' . preg_quote($s, '/') . '/';
56
        }, $positives);
57
 
58
        if (count($matches) > 0) {
59
            $nonInput = $matches[2];
60
 
61
            $negatedString = preg_replace(
62
                '/' . preg_quote($nonInput, '/') . '/',
63
                preg_replace(
64
                    $positives,
65
                    $negatives,
66
                    $nonInput
67
                ),
68
                $string
69
            );
70
        } else {
71
            $negatedString = preg_replace(
72
                $positives,
73
                $negatives,
74
                $string
75
            );
76
        }
77
 
78
        return $negatedString;
79
    }
80
 
81
    /**
82
     * Returns the name of this operator.
83
     */
84
    public function operator(): string
85
    {
86
        return 'not';
87
    }
88
 
89
    /**
90
     * Returns this operator's precedence.
91
     *
92
     * @see https://www.php.net/manual/en/language.operators.precedence.php
93
     */
94
    public function precedence(): int
95
    {
96
        return 5;
97
    }
98
 
99
    /**
100
     * Evaluates the constraint for parameter $other. Returns true if the
101
     * constraint is met, false otherwise.
102
     *
103
     * @param mixed $other value or object to evaluate
104
     */
105
    protected function matches($other): bool
106
    {
107
        return !$this->constraint()->evaluate($other, '', true);
108
    }
109
 
110
    /**
111
     * Applies additional transformation to strings returned by toString() or
112
     * failureDescription().
113
     */
114
    protected function transformString(string $string): string
115
    {
116
        return self::negate($string);
117
    }
118
 
119
    /**
120
     * Reduces the sub-expression starting at $this by skipping degenerate
121
     * sub-expression and returns first descendant constraint that starts
122
     * a non-reducible sub-expression.
123
     *
124
     * See Constraint::reduce() for more.
125
     */
126
    protected function reduce(): Constraint
127
    {
128
        $constraint = $this->constraint();
129
 
130
        if ($constraint instanceof self) {
131
            return $constraint->constraint()->reduce();
132
        }
133
 
134
        return parent::reduce();
135
    }
136
}