Skip to content

Commit

Permalink
bot: fix cs [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil authored and kbond committed May 14, 2023
1 parent 8e856f4 commit 0b4f36e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
11 changes: 10 additions & 1 deletion src/PhpStan/AnonymousFactoryTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
Expand All @@ -27,7 +36,7 @@ public function getClass(): string

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'Zenstruck\Foundry\anonymous';
return 'Zenstruck\Foundry\anonymous' === $functionReflection->getName();
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?\PHPStan\Type\Type
Expand Down
13 changes: 10 additions & 3 deletions src/PhpStan/AnonymousHelpersTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
Expand All @@ -12,8 +21,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\ObjectType;
use Zenstruck\Foundry\BaseFactory;
use Zenstruck\Foundry\Object\ObjectFactory;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;
Expand Down Expand Up @@ -69,7 +76,7 @@ private function extractTargetClass(FuncCall $functionCall): ?string
default => null
};

if ($argPosition === null) {
if (null === $argPosition) {
return null;
}

Expand Down
25 changes: 16 additions & 9 deletions src/PhpStan/FactoryMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
Expand All @@ -25,11 +33,10 @@ public function __construct(
private string $factoryClass,
/** @var class-string */
private string $targetClass,
)
{
) {
}

public static function getFactoryMetadata(CallLike $methodCall, Scope $scope): ?FactoryMetadata
public static function getFactoryMetadata(CallLike $methodCall, Scope $scope): ?self
{
$type = match (true) {
$methodCall instanceof MethodCall => $scope->getType($methodCall->var),
Expand All @@ -42,11 +49,11 @@ public static function getFactoryMetadata(CallLike $methodCall, Scope $scope): ?
}

if ($type instanceof GenericObjectType) {
if (\count($type->getTypes()) !== 1 || !$type->getTypes()[0] instanceof ObjectType) {
if (1 !== \count($type->getTypes()) || !$type->getTypes()[0] instanceof ObjectType) {
return null;
}

return new FactoryMetadata($type->getClassName(), $type->getTypes()[0]->getClassName());
return new self($type->getClassName(), $type->getTypes()[0]->getClassName());
}

try {
Expand All @@ -59,7 +66,7 @@ public static function getFactoryMetadata(CallLike $methodCall, Scope $scope): ?
return null;
}

return new FactoryMetadata($type->getClassName(), $type->getClassName()::class());
return new self($type->getClassName(), $type->getClassName()::class());
}

public function getSingleResultType(): ObjectType
Expand All @@ -82,7 +89,7 @@ public function getFactoryCollectionResultType(): GenericObjectType
return new GenericObjectType(
FactoryCollection::class,
[
$this->getSingleResultType()
$this->getSingleResultType(),
]
);
}
Expand All @@ -102,6 +109,6 @@ private function getProxySingleResult(): GenericObjectType

private function hasPersistence(): bool
{
return is_a($this->factoryClass, PersistentObjectFactory::class, true);
return \is_a($this->factoryClass, PersistentObjectFactory::class, true);
}
}
9 changes: 9 additions & 0 deletions src/PhpStan/FactoryMethodsTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use PhpParser\Node\Expr\MethodCall;
Expand Down
11 changes: 10 additions & 1 deletion src/PhpStan/FactoryRepositoryMethodTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use Doctrine\ODM\MongoDB\Mapping\Annotations\Document as ODMDocumentAttribute;
Expand All @@ -25,7 +34,7 @@ public function getClass(): string

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'repository';
return 'repository' === $methodReflection->getName();
}

public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type
Expand Down
11 changes: 10 additions & 1 deletion src/PhpStan/FactoryStaticMethodsTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\PhpStan;

use PhpParser\Node\Expr\StaticCall;
Expand All @@ -20,7 +29,7 @@ public function getClass(): string

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return in_array(
return \in_array(
$methodReflection->getName(),
[
'createOne',
Expand Down
11 changes: 10 additions & 1 deletion tests/PhpStan/DynamicTypesResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

declare(strict_types=1);

/*
* This file is part of the zenstruck/foundry package.
*
* (c) Kevin Bond <kevinbond@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zenstruck\Foundry\Tests\PhpStan;

use PHPStan\Testing\TypeInferenceTestCase;
Expand All @@ -20,7 +29,7 @@ public function typesProvider(): iterable
* @test
* @dataProvider typesProvider
*/
public function itCanResolveTests(string $assertType, string $file, mixed ...$args): void
public function it_can_resolve_tests(string $assertType, string $file, mixed ...$args): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
use Zenstruck\Foundry\Tests\Fixtures\Entity\Category;
use Zenstruck\Foundry\Tests\Fixtures\Entity\Post;
use Zenstruck\Foundry\Tests\Fixtures\Factories\PostFactory;

use Zenstruck\Foundry\Tests\Fixtures\Factories\UserFactory;

use function Zenstruck\Foundry\anonymous;
use function Zenstruck\Foundry\lazy;

Expand Down

0 comments on commit 0b4f36e

Please sign in to comment.