|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection; |
| 4 | + |
| 5 | +use PHPStan\Testing\PHPStanTestCase; |
| 6 | +use PHPStan\Type\Generic\GenericObjectType; |
| 7 | +use PHPStan\Type\IntegerType; |
| 8 | +use PHPStan\Type\VerbosityLevel; |
| 9 | +use function count; |
| 10 | +use const PHP_VERSION_ID; |
| 11 | + |
| 12 | +class ClassReflectionPropertyHooksTest extends PHPStanTestCase |
| 13 | +{ |
| 14 | + |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + if (PHP_VERSION_ID >= 80400) { |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + self::markTestSkipped('Test requires PHP 8.4'); |
| 22 | + } |
| 23 | + |
| 24 | + public static function dataPropertyHooks(): iterable |
| 25 | + { |
| 26 | + $reflectionProvider = self::createReflectionProvider(); |
| 27 | + |
| 28 | + yield [ |
| 29 | + $reflectionProvider->getClass('PropertyHooksTypes\\Foo'), |
| 30 | + 'i', |
| 31 | + 'set', |
| 32 | + ['int'], |
| 33 | + 'void', |
| 34 | + true, |
| 35 | + ]; |
| 36 | + |
| 37 | + yield [ |
| 38 | + $reflectionProvider->getClass('PropertyHooksTypes\\Foo'), |
| 39 | + 'i', |
| 40 | + 'get', |
| 41 | + [], |
| 42 | + 'int', |
| 43 | + true, |
| 44 | + ]; |
| 45 | + |
| 46 | + yield [ |
| 47 | + $reflectionProvider->getClass('PropertyHooksTypes\\Foo'), |
| 48 | + 'l', |
| 49 | + 'get', |
| 50 | + [], |
| 51 | + 'array<string>', |
| 52 | + true, |
| 53 | + ]; |
| 54 | + |
| 55 | + yield [ |
| 56 | + $reflectionProvider->getClass('PropertyHooksTypes\\Foo'), |
| 57 | + 'n', |
| 58 | + 'set', |
| 59 | + ['array<string>|int'], |
| 60 | + 'void', |
| 61 | + true, |
| 62 | + ]; |
| 63 | + |
| 64 | + yield [ |
| 65 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooShort'), |
| 66 | + 'i', |
| 67 | + 'set', |
| 68 | + ['int'], |
| 69 | + 'void', |
| 70 | + false, |
| 71 | + ]; |
| 72 | + |
| 73 | + yield [ |
| 74 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooShort'), |
| 75 | + 'k', |
| 76 | + 'set', |
| 77 | + ['int|string'], |
| 78 | + 'void', |
| 79 | + false, |
| 80 | + ]; |
| 81 | + |
| 82 | + yield [ |
| 83 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooShort'), |
| 84 | + 'l', |
| 85 | + 'set', |
| 86 | + ['array<string>'], |
| 87 | + 'void', |
| 88 | + false, |
| 89 | + ]; |
| 90 | + |
| 91 | + yield [ |
| 92 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooShort'), |
| 93 | + 'm', |
| 94 | + 'set', |
| 95 | + ['array<string>'], |
| 96 | + 'void', |
| 97 | + false, |
| 98 | + ]; |
| 99 | + |
| 100 | + yield [ |
| 101 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooShort'), |
| 102 | + 'n', |
| 103 | + 'set', |
| 104 | + ['array<string>|int'], |
| 105 | + 'void', |
| 106 | + false, |
| 107 | + ]; |
| 108 | + |
| 109 | + yield [ |
| 110 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 111 | + 'i', |
| 112 | + 'set', |
| 113 | + ['int'], |
| 114 | + 'void', |
| 115 | + true, |
| 116 | + ]; |
| 117 | + |
| 118 | + yield [ |
| 119 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 120 | + 'j', |
| 121 | + 'set', |
| 122 | + ['int'], |
| 123 | + 'void', |
| 124 | + true, |
| 125 | + ]; |
| 126 | + |
| 127 | + yield [ |
| 128 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 129 | + 'k', |
| 130 | + 'set', |
| 131 | + ['int|string'], |
| 132 | + 'void', |
| 133 | + true, |
| 134 | + ]; |
| 135 | + |
| 136 | + yield [ |
| 137 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 138 | + 'l', |
| 139 | + 'set', |
| 140 | + ['array<string>'], |
| 141 | + 'void', |
| 142 | + true, |
| 143 | + ]; |
| 144 | + |
| 145 | + yield [ |
| 146 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 147 | + 'l', |
| 148 | + 'get', |
| 149 | + [], |
| 150 | + 'array<string>', |
| 151 | + true, |
| 152 | + ]; |
| 153 | + |
| 154 | + yield [ |
| 155 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 156 | + 'm', |
| 157 | + 'set', |
| 158 | + ['array<string>'], |
| 159 | + 'void', |
| 160 | + true, |
| 161 | + ]; |
| 162 | + |
| 163 | + yield [ |
| 164 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructor'), |
| 165 | + 'n', |
| 166 | + 'set', |
| 167 | + ['array<string>|int'], |
| 168 | + 'void', |
| 169 | + true, |
| 170 | + ]; |
| 171 | + |
| 172 | + yield [ |
| 173 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'), |
| 174 | + 'l', |
| 175 | + 'set', |
| 176 | + ['array<string>'], |
| 177 | + 'void', |
| 178 | + true, |
| 179 | + ]; |
| 180 | + |
| 181 | + yield [ |
| 182 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'), |
| 183 | + 'l', |
| 184 | + 'get', |
| 185 | + [], |
| 186 | + 'array<string>', |
| 187 | + true, |
| 188 | + ]; |
| 189 | + |
| 190 | + yield [ |
| 191 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooConstructorWithParam'), |
| 192 | + 'm', |
| 193 | + 'set', |
| 194 | + ['array<string>'], |
| 195 | + 'void', |
| 196 | + true, |
| 197 | + ]; |
| 198 | + |
| 199 | + yield [ |
| 200 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 201 | + 'm', |
| 202 | + 'set', |
| 203 | + ['array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>'], |
| 204 | + 'void', |
| 205 | + true, |
| 206 | + ]; |
| 207 | + |
| 208 | + yield [ |
| 209 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 210 | + 'n', |
| 211 | + 'set', |
| 212 | + ['array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>|int'], |
| 213 | + 'void', |
| 214 | + true, |
| 215 | + ]; |
| 216 | + |
| 217 | + yield [ |
| 218 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 219 | + 'm', |
| 220 | + 'get', |
| 221 | + [], |
| 222 | + 'array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>', |
| 223 | + true, |
| 224 | + ]; |
| 225 | + |
| 226 | + yield [ |
| 227 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 228 | + 'n', |
| 229 | + 'get', |
| 230 | + [], |
| 231 | + 'int', |
| 232 | + true, |
| 233 | + ]; |
| 234 | + |
| 235 | + $specificFooGenerics = (new GenericObjectType('PropertyHooksTypes\\FooGenerics', [new IntegerType()]))->getClassReflection(); |
| 236 | + |
| 237 | + yield [ |
| 238 | + $specificFooGenerics, |
| 239 | + 'n', |
| 240 | + 'set', |
| 241 | + ['array<int>|int'], |
| 242 | + 'void', |
| 243 | + true, |
| 244 | + ]; |
| 245 | + |
| 246 | + yield [ |
| 247 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 248 | + 'n', |
| 249 | + 'get', |
| 250 | + [], |
| 251 | + 'int', |
| 252 | + true, |
| 253 | + ]; |
| 254 | + |
| 255 | + yield [ |
| 256 | + $specificFooGenerics, |
| 257 | + 'm', |
| 258 | + 'set', |
| 259 | + ['array<int>'], |
| 260 | + 'void', |
| 261 | + true, |
| 262 | + ]; |
| 263 | + |
| 264 | + yield [ |
| 265 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenerics'), |
| 266 | + 'm', |
| 267 | + 'get', |
| 268 | + [], |
| 269 | + 'array<T of stdClass (class PropertyHooksTypes\FooGenerics, parameter)>', |
| 270 | + true, |
| 271 | + ]; |
| 272 | + |
| 273 | + yield [ |
| 274 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'), |
| 275 | + 'l', |
| 276 | + 'set', |
| 277 | + ['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'], |
| 278 | + 'void', |
| 279 | + true, |
| 280 | + ]; |
| 281 | + |
| 282 | + yield [ |
| 283 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'), |
| 284 | + 'm', |
| 285 | + 'set', |
| 286 | + ['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>'], |
| 287 | + 'void', |
| 288 | + true, |
| 289 | + ]; |
| 290 | + |
| 291 | + yield [ |
| 292 | + $reflectionProvider->getClass('PropertyHooksTypes\\FooGenericsConstructor'), |
| 293 | + 'n', |
| 294 | + 'set', |
| 295 | + ['array<T of stdClass (class PropertyHooksTypes\FooGenericsConstructor, parameter)>|int'], |
| 296 | + 'void', |
| 297 | + true, |
| 298 | + ]; |
| 299 | + |
| 300 | + $specificFooGenericsConstructor = (new GenericObjectType('PropertyHooksTypes\\FooGenericsConstructor', [new IntegerType()]))->getClassReflection(); |
| 301 | + |
| 302 | + yield [ |
| 303 | + $specificFooGenericsConstructor, |
| 304 | + 'n', |
| 305 | + 'set', |
| 306 | + ['array<int>|int'], |
| 307 | + 'void', |
| 308 | + true, |
| 309 | + ]; |
| 310 | + |
| 311 | + yield [ |
| 312 | + $specificFooGenericsConstructor, |
| 313 | + 'm', |
| 314 | + 'set', |
| 315 | + ['array<int>'], |
| 316 | + 'void', |
| 317 | + true, |
| 318 | + ]; |
| 319 | + |
| 320 | + yield [ |
| 321 | + $specificFooGenericsConstructor, |
| 322 | + 'm', |
| 323 | + 'get', |
| 324 | + [], |
| 325 | + 'array<int>', |
| 326 | + true, |
| 327 | + ]; |
| 328 | + } |
| 329 | + |
| 330 | + /** |
| 331 | + * @dataProvider dataPropertyHooks |
| 332 | + * @param ExtendedPropertyReflection::HOOK_* $hookName |
| 333 | + * @param string[] $parameterTypes |
| 334 | + */ |
| 335 | + public function testPropertyHooks( |
| 336 | + ClassReflection $classReflection, |
| 337 | + string $propertyName, |
| 338 | + string $hookName, |
| 339 | + array $parameterTypes, |
| 340 | + string $returnType, |
| 341 | + bool $isVirtual, |
| 342 | + ): void |
| 343 | + { |
| 344 | + $propertyReflection = $classReflection->getNativeProperty($propertyName); |
| 345 | + $this->assertSame($isVirtual, $propertyReflection->isVirtual()->yes()); |
| 346 | + |
| 347 | + $hookReflection = $propertyReflection->getHook($hookName); |
| 348 | + $hookVariant = $hookReflection->getOnlyVariant(); |
| 349 | + $this->assertSame($returnType, $hookVariant->getReturnType()->describe(VerbosityLevel::precise())); |
| 350 | + $this->assertCount(count($parameterTypes), $hookVariant->getParameters()); |
| 351 | + |
| 352 | + foreach ($hookVariant->getParameters() as $i => $parameter) { |
| 353 | + $this->assertSame($parameterTypes[$i], $parameter->getType()->describe(VerbosityLevel::precise())); |
| 354 | + } |
| 355 | + } |
| 356 | + |
| 357 | +} |
0 commit comments