Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ includes:
- extension.neon

parameters:
autoload_files:
scanFiles:
- vendor/yiisoft/yii2/Yii.php

yii2:
Expand Down
13 changes: 4 additions & 9 deletions src/ServiceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class ServiceMap
private $services = [];

/**
* @var (string[]|object)[]
* @var array<string, string>
*/
private $components = [];

Expand All @@ -38,7 +38,7 @@ public function __construct(string $configPath)

foreach ($config['components'] ?? [] as $id => $component) {
if (is_object($component)) {
$this->components[$id] = $component;
$this->components[$id] = \get_class($component);
continue;
}

Expand All @@ -47,7 +47,7 @@ public function __construct(string $configPath)
}

if (null !== $class = $component['class'] ?? null) {
$this->components[$id]['class'] = $class;
$this->components[$id] = $class;
}
}
}
Expand All @@ -63,12 +63,7 @@ public function getServiceClassFromNode(Node $node): ?string

public function getComponentClassById(string $id): ?string
{
// Special case in which the component is already initialized
if (isset($this->components[$id]) && is_object($this->components[$id])) {
return get_class($this->components[$id]);
}

return $this->components[$id]['class'] ?? null;
return $this->components[$id] ?? null;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Type/ActiveQueryDynamicMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -61,13 +60,13 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
if ($methodName === 'one') {
return TypeCombinator::union(
new NullType(),
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ObjectType($calledOnType->getModelClass())
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ActiveRecordObjectType($calledOnType->getModelClass())
);
}

return new ArrayType(
new IntegerType(),
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ObjectType($calledOnType->getModelClass())
$calledOnType->isAsArray() ? new ArrayType(new StringType(), new MixedType()) : new ActiveRecordObjectType($calledOnType->getModelClass())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

Expand All @@ -36,7 +35,7 @@ public function getTypeFromStaticMethodCall(MethodReflection $methodReflection,
if ($methodName === 'findOne') {
return TypeCombinator::union(
new NullType(),
new ObjectType($name)
new ActiveRecordObjectType($name)
);
}

Expand Down
36 changes: 36 additions & 0 deletions src/Type/ActiveRecordObjectType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Proget\PHPStan\Yii2\Type;

use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

class ActiveRecordObjectType extends ObjectType
{
public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
if (!$offsetType instanceof ConstantStringType) {
return TrinaryLogic::createNo();
}

if ($this->isInstanceOf(\ArrayAccess::class)->yes()) {
return TrinaryLogic::createFromBoolean($this->hasProperty($offsetType->getValue())->yes());
}

return parent::hasOffsetValueType($offsetType);
}

public function setOffsetValueType(?Type $offsetType, Type $valueType): Type
{
if ($offsetType instanceof ConstantStringType && $this->hasProperty($offsetType->getValue())->no()) {
return new ErrorType();
}

return $this;
}
}
16 changes: 8 additions & 8 deletions tests/ServiceMapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public function testItLoadsServicesAndComponents(): void
{
$serviceMap = new ServiceMap(__DIR__.DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.'yii-config-valid.php');

$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('singleton-closure')));
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('singleton-service')));
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('singleton-nested-service-class')));
self::assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('singleton-closure')));
self::assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('singleton-service')));
self::assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('singleton-nested-service-class')));

$this->assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('closure')));
$this->assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('service')));
$this->assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));
self::assertSame(\SplStack::class, $serviceMap->getServiceClassFromNode(new String_('closure')));
self::assertSame(\SplObjectStorage::class, $serviceMap->getServiceClassFromNode(new String_('service')));
self::assertSame(\SplFileInfo::class, $serviceMap->getServiceClassFromNode(new String_('nested-service-class')));

$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customComponent'));
$this->assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customInitializedComponent'));
self::assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customComponent'));
self::assertSame(MyActiveRecord::class, $serviceMap->getComponentClassById('customInitializedComponent'));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/Yii/MyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ final class MyController extends \yii\web\Controller
{
public function actionMy(): void
{
$offsetProp = 'flag';
$flag = false;
$record = MyActiveRecord::find()->where(['flag' => \Yii::$app->request->post('flag', true)])->one();
if ($record) {
$record->flag = false;
$flag = $record[$offsetProp];
$record[$offsetProp] = true;
$record->save();
}

$record = MyActiveRecord::findOne(['condition']);
if ($record) {
$flag = $record->flag;
$flag = $record['flag'];
}

$records = MyActiveRecord::find()->asArray()->where(['flag' => \Yii::$app->request->post('flag', true)])->all();
Expand All @@ -33,6 +38,7 @@ public function actionMy(): void
foreach ($records as $record) {
$flag = $record->flag;
$flag = $record['flag'];
$record['flag'] = true;
}

\Yii::$app->response->data = \Yii::$app->request->rawBody;
Expand Down