|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SelfContradiction; |
| 4 | + |
| 5 | +use PhpParser\Node\Expr; |
| 6 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 7 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 8 | +use PhpParser\Node\Expr\ConstFetch; |
| 9 | +use PhpParser\Node\Scalar; |
| 10 | + |
| 11 | +class AssertStaticCall { |
| 12 | + /** |
| 13 | + * @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node |
| 14 | + */ |
| 15 | + private static function isSubjectNode(Expr $node): bool |
| 16 | + { |
| 17 | + return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null |
| 22 | + */ |
| 23 | + private function getSubjectAndValue(Identical $comparison): ?array |
| 24 | + { |
| 25 | + if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) { |
| 26 | + return ['subject' => $comparison->right, 'value' => $comparison->left]; |
| 27 | + } |
| 28 | + |
| 29 | + if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) { |
| 30 | + return ['subject' => $comparison->left, 'value' => $comparison->right]; |
| 31 | + } |
| 32 | + |
| 33 | + return null; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +class AssertMethodCall { |
| 38 | + /** |
| 39 | + * @phpstan-assert-if-true Scalar|ClassConstFetch|ConstFetch $node |
| 40 | + */ |
| 41 | + private function isSubjectNode(Expr $node): bool |
| 42 | + { |
| 43 | + return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null |
| 48 | + */ |
| 49 | + private function getSubjectAndValue(Identical $comparison): ?array |
| 50 | + { |
| 51 | + if ($this->isSubjectNode($comparison->left) && !$this->isSubjectNode($comparison->left)) { |
| 52 | + return ['subject' => $comparison->right, 'value' => $comparison->left]; |
| 53 | + } |
| 54 | + |
| 55 | + if (!$this->isSubjectNode($comparison->left) && $this->isSubjectNode($comparison->right)) { |
| 56 | + return ['subject' => $comparison->left, 'value' => $comparison->right]; |
| 57 | + } |
| 58 | + |
| 59 | + return null; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +class AssertStaticConditionalReturn { |
| 64 | + /** |
| 65 | + * @return ($node is Scalar|ClassConstFetch|ConstFetch ? true : false) |
| 66 | + */ |
| 67 | + private static function isSubjectNode(Expr $node): bool |
| 68 | + { |
| 69 | + return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null |
| 74 | + */ |
| 75 | + private function getSubjectAndValue(Identical $comparison): ?array |
| 76 | + { |
| 77 | + if (self::isSubjectNode($comparison->left) && !self::isSubjectNode($comparison->left)) { |
| 78 | + return ['subject' => $comparison->right, 'value' => $comparison->left]; |
| 79 | + } |
| 80 | + |
| 81 | + if (!self::isSubjectNode($comparison->left) && self::isSubjectNode($comparison->right)) { |
| 82 | + return ['subject' => $comparison->left, 'value' => $comparison->right]; |
| 83 | + } |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +class AssertInstanceConditionalReturn { |
| 90 | + /** |
| 91 | + * @return ($node is Scalar|ClassConstFetch|ConstFetch ? true : false) |
| 92 | + */ |
| 93 | + private function isSubjectNode(Expr $node): bool |
| 94 | + { |
| 95 | + return $node instanceof Scalar || $node instanceof ClassConstFetch || $node instanceof ConstFetch; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array{subject: Expr, value: Scalar|ClassConstFetch|ConstFetch}|null |
| 100 | + */ |
| 101 | + private function getSubjectAndValue(Identical $comparison): ?array |
| 102 | + { |
| 103 | + if ($this->isSubjectNode($comparison->left) && !$this->isSubjectNode($comparison->left)) { |
| 104 | + return ['subject' => $comparison->right, 'value' => $comparison->left]; |
| 105 | + } |
| 106 | + |
| 107 | + if (!$this->isSubjectNode($comparison->left) && $this->isSubjectNode($comparison->right)) { |
| 108 | + return ['subject' => $comparison->left, 'value' => $comparison->right]; |
| 109 | + } |
| 110 | + |
| 111 | + return null; |
| 112 | + } |
| 113 | +} |
0 commit comments