|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace test\PhpStaticAnalysis\PHPStanExtension\data\Assert; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use PhpStaticAnalysis\Attributes\Assert; |
| 7 | + |
| 8 | +class MethodAssertAttribute |
| 9 | +{ |
| 10 | + #[Assert(name: 'string')] // checks name is string |
| 11 | + public function checkString(mixed $name): void |
| 12 | + { |
| 13 | + if (!is_string($name)) { |
| 14 | + throw new Exception(); |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + #[Assert(exception: Exception::class)] |
| 19 | + public function checkException(mixed $exception): void |
| 20 | + { |
| 21 | + if (!$exception instanceof Exception) { |
| 22 | + throw new Exception(); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + #[Assert('string $name')] |
| 27 | + public function checkOtherString(mixed $name): void |
| 28 | + { |
| 29 | + if (!is_string($name)) { |
| 30 | + throw new Exception(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @deprecated |
| 36 | + */ |
| 37 | + #[Assert(name: 'string')] |
| 38 | + public function checkAnotherString(mixed $name): void |
| 39 | + { |
| 40 | + if (!is_string($name)) { |
| 41 | + throw new Exception(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @assert int $name |
| 47 | + */ |
| 48 | + #[Assert(name: 'string')] |
| 49 | + public function checkEvenMoreString(mixed $name): void |
| 50 | + { |
| 51 | + if (!is_string($name)) { |
| 52 | + throw new Exception(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + #[Assert( |
| 57 | + name1: 'string', |
| 58 | + name2: 'string' |
| 59 | + )] |
| 60 | + public function checkStrings(mixed $name1, mixed $name2): void |
| 61 | + { |
| 62 | + if (!is_string($name1) || !is_string($name2)) { |
| 63 | + throw new Exception(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + #[Assert(name1: 'string')] |
| 68 | + #[Assert(name2: 'string')] |
| 69 | + public function checkOtherStrings(mixed $name1, mixed $name2): void |
| 70 | + { |
| 71 | + if (!is_string($name1) || !is_string($name2)) { |
| 72 | + throw new Exception(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @assert string $name |
| 78 | + */ |
| 79 | + public function checkMoreAndMoreString(mixed $name): void |
| 80 | + { |
| 81 | + if (!is_string($name)) { |
| 82 | + throw new Exception(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public function checkStringInParam( |
| 87 | + #[Assert('string')] |
| 88 | + mixed $name |
| 89 | + ): void { |
| 90 | + if (!is_string($name)) { |
| 91 | + throw new Exception(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public function checkStringInParamWithName( |
| 96 | + #[Assert(name: 'string')] |
| 97 | + mixed $name |
| 98 | + ): void { |
| 99 | + if (!is_string($name)) { |
| 100 | + throw new Exception(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public function checkStringInTwoParams( |
| 105 | + #[Assert('string')] |
| 106 | + mixed $name1, |
| 107 | + #[Assert('string')] |
| 108 | + mixed $name2 |
| 109 | + ): void { |
| 110 | + if (!is_string($name1) || !is_string($name2)) { |
| 111 | + throw new Exception(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments