PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpunit/phpunit/src/Framework/Constraint/Operator/LogicalNot.php

https://gitlab.com/madwanz64/laravel
PHP | 136 lines | 85 code | 15 blank | 36 comment | 3 complexity | 4d5853ecdba4962c2e76f9a70142e227 MD5 | raw file
  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. use function array_map;
  12. use function count;
  13. use function preg_match;
  14. use function preg_quote;
  15. use function preg_replace;
  16. /**
  17. * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
  18. */
  19. final class LogicalNot extends UnaryOperator
  20. {
  21. public static function negate(string $string): string
  22. {
  23. $positives = [
  24. 'contains ',
  25. 'exists',
  26. 'has ',
  27. 'is ',
  28. 'are ',
  29. 'matches ',
  30. 'starts with ',
  31. 'ends with ',
  32. 'reference ',
  33. 'not not ',
  34. ];
  35. $negatives = [
  36. 'does not contain ',
  37. 'does not exist',
  38. 'does not have ',
  39. 'is not ',
  40. 'are not ',
  41. 'does not match ',
  42. 'starts not with ',
  43. 'ends not with ',
  44. 'don\'t reference ',
  45. 'not ',
  46. ];
  47. preg_match('/(\'[\w\W]*\')([\w\W]*)("[\w\W]*")/i', $string, $matches);
  48. $positives = array_map(static function (string $s)
  49. {
  50. return '/\\b' . preg_quote($s, '/') . '/';
  51. }, $positives);
  52. if (count($matches) > 0) {
  53. $nonInput = $matches[2];
  54. $negatedString = preg_replace(
  55. '/' . preg_quote($nonInput, '/') . '/',
  56. preg_replace(
  57. $positives,
  58. $negatives,
  59. $nonInput
  60. ),
  61. $string
  62. );
  63. } else {
  64. $negatedString = preg_replace(
  65. $positives,
  66. $negatives,
  67. $string
  68. );
  69. }
  70. return $negatedString;
  71. }
  72. /**
  73. * Returns the name of this operator.
  74. */
  75. public function operator(): string
  76. {
  77. return 'not';
  78. }
  79. /**
  80. * Returns this operator's precedence.
  81. *
  82. * @see https://www.php.net/manual/en/language.operators.precedence.php
  83. */
  84. public function precedence(): int
  85. {
  86. return 5;
  87. }
  88. /**
  89. * Evaluates the constraint for parameter $other. Returns true if the
  90. * constraint is met, false otherwise.
  91. *
  92. * @param mixed $other value or object to evaluate
  93. */
  94. protected function matches($other): bool
  95. {
  96. return !$this->constraint()->evaluate($other, '', true);
  97. }
  98. /**
  99. * Applies additional transformation to strings returned by toString() or
  100. * failureDescription().
  101. */
  102. protected function transformString(string $string): string
  103. {
  104. return self::negate($string);
  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. $constraint = $this->constraint();
  116. if ($constraint instanceof self) {
  117. return $constraint->constraint()->reduce();
  118. }
  119. return parent::reduce();
  120. }
  121. }