|
4 | 4 |
|
5 | 5 | namespace Larastan\Larastan\Types\ModelProperty;
|
6 | 6 |
|
| 7 | +use Illuminate\Database\Eloquent\Model; |
7 | 8 | use Illuminate\Support\Str;
|
| 9 | +use Larastan\Larastan\Support\ModelHelper; |
8 | 10 | use Larastan\Larastan\Properties\ModelDatabaseHelper;
|
9 | 11 | use PHPStan\Type\AcceptsResult;
|
10 | 12 | use PHPStan\Type\CompoundType;
|
|
30 | 32 |
|
31 | 33 | class GenericModelPropertyType extends StringType
|
32 | 34 | {
|
33 |
| - public function __construct(private Type $type, private ModelDatabaseHelper $modelDatabaseHelper) |
| 35 | + public function __construct( |
| 36 | + private Type $type, |
| 37 | + private ModelDatabaseHelper $modelDatabaseHelper, |
| 38 | + private ModelHelper $modelHelper, |
| 39 | + ) |
34 | 40 | {
|
35 | 41 | parent::__construct();
|
36 | 42 | }
|
@@ -76,12 +82,14 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult
|
76 | 82 | if (str_contains($givenString, '.')) {
|
77 | 83 | $tableName = Str::beforeLast($givenString, '.');
|
78 | 84 | $propertyName = Str::afterLast($givenString, '.');
|
| 85 | + /** @var class-string<Model> $modelClass */ |
| 86 | + $modelClass = $genericType->getObjectClassNames()[0]; |
79 | 87 |
|
80 | 88 | // Assume the connection is the same as the generic model's connection
|
81 | 89 | // or fallback to the default connection
|
82 |
| - $connection = $this->modelDatabaseHelper |
83 |
| - ->getModelInstance($genericType->getObjectClassNames()[0]) |
84 |
| - ?->getConnectionName() |
| 90 | + $connection = $this->modelHelper |
| 91 | + ->getModelInstance($modelClass) |
| 92 | + ->getConnectionName() |
85 | 93 | ?? $this->modelDatabaseHelper->getDefaultConnection();
|
86 | 94 |
|
87 | 95 | if (! isset($this->modelDatabaseHelper->connections[$connection]->tables[$tableName]->columns[$propertyName])) {
|
@@ -144,7 +152,7 @@ public function traverse(callable $cb): Type
|
144 | 152 | return $this;
|
145 | 153 | }
|
146 | 154 |
|
147 |
| - return new self($newType, $this->modelDatabaseHelper); |
| 155 | + return new self($newType, $this->modelDatabaseHelper, $this->modelHelper); |
148 | 156 | }
|
149 | 157 |
|
150 | 158 | public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
|
@@ -189,6 +197,6 @@ public function getReferencedTemplateTypes(TemplateTypeVariance $positionVarianc
|
189 | 197 | /** @param mixed[] $properties */
|
190 | 198 | public static function __set_state(array $properties): Type
|
191 | 199 | {
|
192 |
| - return new self($properties['type'], $properties['modelDatabaseHelper']); |
| 200 | + return new self($properties['type'], $properties['modelDatabaseHelper'], $properties['modelHelper']); |
193 | 201 | }
|
194 | 202 | }
|
0 commit comments