|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TypePHP\Tests\TypeChecking\Generics; |
| 6 | + |
| 7 | +use TypePHP\Exception\TypeError; |
| 8 | +use TypePHP\Internal\Config; |
| 9 | +use TypePHP\Tests\Fixtures\Collections\ShopwareCollection; |
| 10 | +use TypePHP\Tests\Fixtures\Collections\ShopwareEntityCollection; |
| 11 | +use TypePHP\Tests\Fixtures\Collections\ShopwareEntitySearchResult; |
| 12 | +use TypePHP\Tests\Fixtures\Collections\SpecificDogSearchResult; |
| 13 | +use TypePHP\Tests\Fixtures\Domain\Car; |
| 14 | +use TypePHP\Tests\Fixtures\Domain\Dog; |
| 15 | + |
| 16 | +/** |
| 17 | + * @template TEntityCollection of ShopwareCollection |
| 18 | + */ |
| 19 | +class ClassTemplateInlineVarRepository |
| 20 | +{ |
| 21 | + /** |
| 22 | + * Case 1: Exact Shopware Line 129 Pattern Named @var $result on a return expression constructing SearchResult |
| 23 | + */ |
| 24 | + public function searchConstructsResult(): object |
| 25 | + { |
| 26 | + $result = new ShopwareEntityCollection(); |
| 27 | + |
| 28 | + /** @var TEntityCollection $result */ |
| 29 | + return new ShopwareEntitySearchResult($result); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Case 2: Named @var $result where the return expression IS $result |
| 34 | + */ |
| 35 | + public function searchDirectReturn(): object |
| 36 | + { |
| 37 | + /** @var TEntityCollection $result */ |
| 38 | + $result = new ShopwareEntityCollection(); |
| 39 | + |
| 40 | + return $result; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Case 3: Unnamed @var on return statement |
| 45 | + */ |
| 46 | + public function searchUnnamedReturn(): object |
| 47 | + { |
| 48 | + /** @var TEntityCollection */ |
| 49 | + return new ShopwareEntityCollection(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Case 4: Template used inside Generic Container on inline variable |
| 54 | + */ |
| 55 | + public function searchWithGenericContainer(): object |
| 56 | + { |
| 57 | + $collection = new ShopwareEntityCollection(); |
| 58 | + |
| 59 | + /** @var ShopwareEntitySearchResult<TEntityCollection> $searchResult */ |
| 60 | + $searchResult = new ShopwareEntitySearchResult($collection); |
| 61 | + |
| 62 | + return $searchResult; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Case 5: Method-level generic template in inline variable |
| 67 | + * |
| 68 | + * @template TItem of object |
| 69 | + * |
| 70 | + * @param TItem $item |
| 71 | + */ |
| 72 | + public function processItem(object $item): object |
| 73 | + { |
| 74 | + /** @var TItem $localItem */ |
| 75 | + $localItem = $item; |
| 76 | + |
| 77 | + return $localItem; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Case 6: Assigning an invalid object violating the template upper bound |
| 82 | + */ |
| 83 | + public function assignInvalidObject(): void |
| 84 | + { |
| 85 | + /** @var TEntityCollection $bad */ |
| 86 | + $bad = new Car(); // Car is not a ShopwareCollection! |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Subclass binding TEntityCollection to SpecificDogSearchResult |
| 92 | + * |
| 93 | + * @extends ClassTemplateInlineVarRepository<SpecificDogSearchResult> |
| 94 | + */ |
| 95 | +class ConcreteDogRepository extends ClassTemplateInlineVarRepository |
| 96 | +{ |
| 97 | + public function searchSpecificDog(): object |
| 98 | + { |
| 99 | + /** @var TEntityCollection $dogResult */ |
| 100 | + $dogResult = new SpecificDogSearchResult(); |
| 101 | + |
| 102 | + return $dogResult; |
| 103 | + } |
| 104 | + |
| 105 | + public function searchWrongCollection(): object |
| 106 | + { |
| 107 | + /** @var TEntityCollection $wrongResult */ |
| 108 | + $wrongResult = new ShopwareEntityCollection(); |
| 109 | + |
| 110 | + return $wrongResult; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +describe('Class-Level & Method-Level Template Resolution in Inline @var Annotations', function () { |
| 115 | + test('resolves named @var on return expression constructing new object (Exact Shopware Line 129)', function () { |
| 116 | + $repo = new ClassTemplateInlineVarRepository(); |
| 117 | + expect($repo->searchConstructsResult())->toBeInstanceOf(ShopwareEntitySearchResult::class); |
| 118 | + }); |
| 119 | + |
| 120 | + test('resolves named @var where return expression is the variable', function () { |
| 121 | + $repo = new ClassTemplateInlineVarRepository(); |
| 122 | + expect($repo->searchDirectReturn())->toBeInstanceOf(ShopwareEntityCollection::class); |
| 123 | + }); |
| 124 | + |
| 125 | + test('resolves unnamed @var directly on return statement', function () { |
| 126 | + $repo = new ClassTemplateInlineVarRepository(); |
| 127 | + expect($repo->searchUnnamedReturn())->toBeInstanceOf(ShopwareEntityCollection::class); |
| 128 | + }); |
| 129 | + |
| 130 | + test('resolves template used inside generic container on inline @var', function () { |
| 131 | + $repo = new ClassTemplateInlineVarRepository(); |
| 132 | + expect($repo->searchWithGenericContainer())->toBeInstanceOf(ShopwareEntitySearchResult::class); |
| 133 | + }); |
| 134 | + |
| 135 | + test('resolves method-level template in inline @var variable', function () { |
| 136 | + $repo = new ClassTemplateInlineVarRepository(); |
| 137 | + $dog = new Dog(); |
| 138 | + |
| 139 | + expect($repo->processItem($dog))->toBe($dog); |
| 140 | + }); |
| 141 | + |
| 142 | + test('throws TypeError when inline variable violates template upper bound', function () { |
| 143 | + $repo = new ClassTemplateInlineVarRepository(); |
| 144 | + |
| 145 | + expect(fn () => $repo->assignInvalidObject()) |
| 146 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Collections\ShopwareCollection') |
| 147 | + ; |
| 148 | + }); |
| 149 | + |
| 150 | + test('resolves pre-bound template in subclass on inline @var variable', function () { |
| 151 | + $repo = new ConcreteDogRepository(); |
| 152 | + |
| 153 | + expect($repo->searchSpecificDog())->toBeInstanceOf(SpecificDogSearchResult::class); |
| 154 | + |
| 155 | + expect(fn () => $repo->searchWrongCollection()) |
| 156 | + ->toThrow(TypeError::class, 'must be of type TypePHP\Tests\Fixtures\Collections\SpecificDogSearchResult') |
| 157 | + ; |
| 158 | + }); |
| 159 | + |
| 160 | + test('respects inline_vars configuration toggles when disabled', function () { |
| 161 | + try { |
| 162 | + $repo = new ClassTemplateInlineVarRepository(); |
| 163 | + |
| 164 | + expect(fn () => $repo->assignInvalidObject()) |
| 165 | + ->toThrow(TypeError::class) |
| 166 | + ; |
| 167 | + |
| 168 | + Config::set([ |
| 169 | + 'inline_vars' => [ |
| 170 | + 'objects' => false, |
| 171 | + 'generics' => false, |
| 172 | + ], |
| 173 | + ]); |
| 174 | + |
| 175 | + $repo->assignInvalidObject(); |
| 176 | + expect(true)->toBeTrue(); |
| 177 | + } finally { |
| 178 | + Config::reset(); |
| 179 | + } |
| 180 | + }); |
| 181 | +}); |
0 commit comments