Skip to content

Commit c390b9b

Browse files
fix: Enhance ActiveRecordDynamicStaticMethodReturnTypeExtension type inference for ActiveQuery support, and fix phpstan errors max lvl in tests. (#33)
1 parent c74ff83 commit c390b9b

File tree

4 files changed

+29
-17
lines changed

4 files changed

+29
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Bug #27: Enhance error handling in `ServiceMap` for invalid configuration structures and add corresponding test cases (@terabytesoftw)
88
- Bug #28: Refactor component handling in `ServiceMap` to improve variable naming and streamline logic (@terabytesoftw)
99
- Enh #29: Enable strict rules and bleeding edge analysis, and update `README.md` with strict configuration examples (@terabytesoftw)
10+
- Bug #33: Enhance `ActiveRecordDynamicStaticMethodReturnTypeExtension` type inference for `ActiveQuery` support, and fix phpstan errors max lvl in tests (@terabytesoftw)
1011

1112
## 0.2.2 June 04, 2025
1213

src/type/ActiveRecordDynamicStaticMethodReturnTypeExtension.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ public function isStaticMethodSupported(MethodReflection $methodReflection): boo
130130
if (count($classNames) > 0) {
131131
$className = $classNames[0];
132132

133+
if ($className === ActiveQuery::class) {
134+
return true;
135+
}
136+
133137
if ($this->reflectionProvider->hasClass($className)) {
134138
$classReflection = $this->reflectionProvider->getClass($className);
135139

@@ -165,6 +169,7 @@ public function getTypeFromStaticMethodCall(
165169
Scope $scope,
166170
): Type {
167171
$className = $methodCall->class;
172+
168173
$returnType = ParametersAcceptorSelector::selectFromArgs(
169174
$scope,
170175
$methodCall->getArgs(),
@@ -185,6 +190,12 @@ public function getTypeFromStaticMethodCall(
185190
return TypeCombinator::union(new NullType(), new ActiveRecordObjectType($name));
186191
}
187192

188-
return new ActiveQueryObjectType($name, false);
193+
$classNames = $returnType->getObjectClassNames();
194+
195+
if (count($classNames) > 0 && $classNames[0] === ActiveQuery::class) {
196+
return new ActiveQueryObjectType($name, false);
197+
}
198+
199+
return $returnType;
189200
}
190201
}

tests/ServiceMapTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,65 +62,65 @@ public function testItLoadsServicesAndComponents(): void
6262

6363
$serviceMap = new ServiceMap($fixturePath);
6464

65-
$this->assertNull(
65+
self::assertNull(
6666
$serviceMap->getServiceClassFromNode(new String_('non-existent-service')),
6767
'ServiceMap should return \'null\' for a \'non-existent service\'.',
6868
);
69-
$this->assertSame(
69+
self::assertSame(
7070
MyActiveRecord::class,
7171
$serviceMap->getServiceClassFromNode(new String_('singleton-string')),
7272
'ServiceMap should resolve \'singleton-string\' to \'MyActiveRecord::class\'.',
7373
);
74-
$this->assertSame(
74+
self::assertSame(
7575
MyActiveRecord::class,
7676
$serviceMap->getServiceClassFromNode(new String_(MyActiveRecord::class)),
7777
'ServiceMap should resolve \'MyActiveRecord::class\' as a singleton string service.',
7878
);
79-
$this->assertSame(
79+
self::assertSame(
8080
SplStack::class,
8181
$serviceMap->getServiceClassFromNode(new String_('singleton-closure')),
8282
'ServiceMap should resolve \'singleton-closure\' to \'SplStack::class\'.',
8383
);
84-
$this->assertSame(
84+
self::assertSame(
8585
SplObjectStorage::class,
8686
$serviceMap->getServiceClassFromNode(new String_('singleton-service')),
8787
'ServiceMap should resolve \'singleton-service\' to \'SplObjectStorage::class\'.',
8888
);
89-
$this->assertSame(
89+
self::assertSame(
9090
SplFileInfo::class,
9191
$serviceMap->getServiceClassFromNode(new String_('singleton-nested-service-class')),
9292
'ServiceMap should resolve \'singleton-nested-service-class\' to \'SplFileInfo::class\'.',
9393
);
94-
$this->assertSame(
94+
self::assertSame(
9595
SplStack::class,
9696
$serviceMap->getServiceClassFromNode(new String_('closure')),
9797
'ServiceMap should resolve \'closure\' to \'SplStack::class\'.',
9898
);
99-
$this->assertSame(
99+
self::assertSame(
100100
SplObjectStorage::class,
101101
$serviceMap->getServiceClassFromNode(new String_('service')),
102102
'ServiceMap should resolve \'service\' to \'SplObjectStorage::class\'.',
103103
);
104-
$this->assertSame(
104+
self::assertSame(
105105
SplFileInfo::class,
106106
$serviceMap->getServiceClassFromNode(new String_('nested-service-class')),
107107
'ServiceMap should resolve \'nested-service-class\' to \'SplFileInfo::class\'.',
108108
);
109-
$this->assertSame(
109+
self::assertSame(
110110
MyActiveRecord::class,
111111
$serviceMap->getComponentClassById('customComponent'),
112112
'ServiceMap should resolve component id \'customComponent\' to \'MyActiveRecord::class\'.',
113113
);
114-
$this->assertNull(
114+
self::assertNull(
115115
$serviceMap->getComponentClassById('nonExistentComponent'),
116116
'ServiceMap should return \'null\' for a \'non-existent\' component id.',
117117
);
118-
$this->assertSame(
118+
self::assertSame(
119119
MyActiveRecord::class,
120120
$serviceMap->getComponentClassById('customInitializedComponent'),
121121
'ServiceMap should resolve component id \'customInitializedComponent\' to \'MyActiveRecord::class\'.',
122122
);
123-
$this->assertNull(
123+
self::assertNull(
124124
$serviceMap->getComponentClassById('assetManager'),
125125
'ServiceMap should return \'null\' for \'assetManager\' component id as it is not a class but an array.',
126126
);

tests/stub/MyController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function actionMy(): void
4747

4848
$record = MyActiveRecord::find()->where(['flag' => Yii::$app->request->post('flag', true)])->one();
4949

50-
if ($record) {
50+
if ($record !== null) {
5151
$record->flag = false;
5252
$flag = $record[$offsetProp];
5353
$record[$offsetProp] = true;
@@ -56,14 +56,14 @@ public function actionMy(): void
5656

5757
$record = MyActiveRecord::findOne(['condition']);
5858

59-
if ($record) {
59+
if ($record !== null) {
6060
$flag = $record->flag;
6161
$flag = $record['flag'];
6262
}
6363

6464
$record = MyActiveRecord::findBySql('');
6565

66-
if ($record = $record->one()) {
66+
if (($record = $record->one()) !== null) {
6767
$flag = $record->flag;
6868
$flag = $record['flag'];
6969
}

0 commit comments

Comments
 (0)