Skip to content

Commit

Permalink
chore: psalm plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Jul 21, 2023
1 parent 7dfd952 commit 137c23c
Show file tree
Hide file tree
Showing 19 changed files with 457 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ docker-start: ### Start containers

.PHONY: docker-stop
docker-stop: ### Stop containers
@rm $(DOCKER_PHP_CONTAINER_FLAG)
@rm $(DOCKER_PHP_CONTAINER_FLAG) || true
@$(DOCKER_COMPOSE) stop

.PHONY: docker-purge
Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"zenstruck/callback": "^1.1"
},
"require-dev": {
"ext-simplexml": "*",
"bamarni/composer-bin-plugin": "^1.4",
"dama/doctrine-test-bundle": "^7.0",
"doctrine/doctrine-bundle": "^2.5",
Expand Down Expand Up @@ -63,6 +64,14 @@
"target-directory": "bin/tools",
"bin-links": true,
"forward-command": false
},
"phpstan": {
"includes": [
"phpstan-foundry.neon"
]
},
"psalm": {
"pluginClass": "Zenstruck\\Foundry\\Psalm\\FoundryPlugin"
}
},
"minimum-stability": "dev",
Expand Down
30 changes: 0 additions & 30 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ parameters:
count: 1
path: src/AnonymousFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\AnonymousFactory\\:\\:many\\(\\) should return Zenstruck\\\\Foundry\\\\FactoryCollection\\<TModel of object\\> but returns Zenstruck\\\\Foundry\\\\FactoryCollection\\<object\\>\\.$#"
count: 1
path: src/AnonymousFactory.php

-
message: "#^Method Zenstruck\\\\Foundry\\\\AnonymousFactory\\:\\:sequence\\(\\) should return Zenstruck\\\\Foundry\\\\FactoryCollection\\<TModel of object\\> but returns Zenstruck\\\\Foundry\\\\FactoryCollection\\<object\\>\\.$#"
count: 1
path: src/AnonymousFactory.php

-
message: "#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string\\|null given\\.$#"
count: 1
Expand Down Expand Up @@ -89,23 +79,3 @@ parameters:
message: "#^Parameter \\#2 \\$configuration of static method Zenstruck\\\\Foundry\\\\BaseFactory\\<mixed\\>\\:\\:boot\\(\\) expects Zenstruck\\\\Foundry\\\\Configuration, object\\|null given\\.$#"
count: 1
path: src/ZenstruckFoundryBundle.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\create\\(\\) should return \\(T of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<T of object\\> but returns object\\.$#"
count: 1
path: src/functions.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\create_many\\(\\) should return array\\<int, \\(T of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<T of object\\>\\> but returns array\\<int, object\\>\\.$#"
count: 1
path: src/functions.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\instantiate\\(\\) should return \\(T of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<T of object\\> but returns object\\.$#"
count: 1
path: src/functions.php

-
message: "#^Function Zenstruck\\\\Foundry\\\\instantiate_many\\(\\) should return array\\<int, \\(T of object\\)\\|Zenstruck\\\\Foundry\\\\Proxy\\<T of object\\>\\> but returns array\\<int, object\\>\\.$#"
count: 1
path: src/functions.php
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ parameters:
excludePaths:
- ./src/Bundle/Resources
- ./src/PhpStan
- ./src/Psalm
10 changes: 8 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
xsi:schemaLocation="https://getpsalm.org/schema/config bin/tools/psalm/vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
autoloader="vendor/autoload.php"
>
<projectFiles>
<file name="tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php" />
<file name="tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php" />
<file name="tests/Fixtures/Maker/expected/can_create_factory_for_entity_with_repository_with_data_set_psalm.php"/>
<file name="tests/Fixtures/Maker/expected/can_create_factory_with_static_analysis_annotations_with_data_set_psalm.php"/>
<file name="tests/Fixtures/Psalm/test-types-with-persistence.php"/>
<file name="tests/Fixtures/Psalm/test-types-without-persistence.php"/>
</projectFiles>
<plugins>
<pluginClass class="Zenstruck\Foundry\Psalm\FoundryPlugin"/>
</plugins>
</psalm>
21 changes: 10 additions & 11 deletions src/BaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
*
* @template T
*
* @method static list<T> createMany(int $number, Attributes $attributes = [])
*
* @phpstan-type Parameters array<string,mixed>
* @phpstan-type Attributes Parameters|(callable():Parameters)
* @phpstan-type SequenceAttributes iterable<Parameters>|(callable(): iterable<Parameters>)
Expand Down Expand Up @@ -51,15 +49,6 @@ public function __call(string $name, array $arguments): array
return $this->many($arguments[0])->create($arguments[1] ?? []);
}

public static function __callStatic(string $name, array $arguments): array
{
if ('createMany' !== $name) {
throw new \BadMethodCallException(\sprintf('Call to undefined static method "%s::%s".', static::class, $name));
}

return static::new()->many($arguments[0])->create($arguments[1] ?? []);
}

/**
* @param Attributes|string $attributes
*/
Expand Down Expand Up @@ -117,6 +106,16 @@ final public static function createOne(array|callable $attributes = []): mixed
*/
abstract public function create(array|callable $attributes = []): mixed;

/**
* @param Attributes $attributes
*
* @return list<T>
*/
public static function createMany(int $min, array|callable $attributes = []): array
{
return static::new()->many($min)->create($attributes);
}

/**
* @param int|null $max If set, when created, the collection will be a random size between $min and $max
*
Expand Down
5 changes: 5 additions & 0 deletions src/Object/ObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ abstract class ObjectFactory extends BaseFactory
*/
abstract public static function class(): string;

/**
* @param Attributes $attributes
*
* @return T
*/
public function create(array|callable $attributes = []): object
{
return $this->normalizeAndInstantiate($attributes)[0];
Expand Down
41 changes: 12 additions & 29 deletions src/Persistence/PersistentObjectFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
*
* @phpstan-type Criteria Proxy<T>|array|mixed
*
* @method static list<Proxy&T> createSequence(SequenceAttributes $sequence)
* @method static list<Proxy&T> createMany(int $number, Attributes $attributes = [])
*
* @phpstan-method static list<Proxy<T>> createSequence(SequenceAttributes $sequence)
* @phpstan-method static list<Proxy<T>> createMany(int $number, Attributes $attributes = [])
*
* @phpstan-import-type Parameters from BaseFactory
* @phpstan-import-type Attributes from BaseFactory
* @phpstan-import-type SequenceAttributes from BaseFactory
Expand Down Expand Up @@ -96,8 +90,7 @@ final public static function persistenceManager(): PersistenceManager
*
* @phpstan-param Criteria $criteria
*
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*
* @throws \RuntimeException If no entity found
*/
Expand All @@ -118,8 +111,7 @@ final public static function assert(): RepositoryAssertions
/**
* @param Parameters $attributes
*
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public static function findOrCreate(array $attributes): object
{
Expand All @@ -134,8 +126,7 @@ final public static function findOrCreate(array $attributes): object
}

/**
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public static function first(string $sortedField = 'id'): object
{
Expand All @@ -147,8 +138,7 @@ final public static function first(string $sortedField = 'id'): object
}

/**
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public static function last(string $sortedField = 'id'): object
{
Expand All @@ -160,8 +150,7 @@ final public static function last(string $sortedField = 'id'): object
}

/**
* @return list<T&Proxy>
* @phpstan-return list<Proxy<T>>
* @return list<Proxy<T>>
*/
final public static function all(): array
{
Expand All @@ -171,8 +160,7 @@ final public static function all(): array
/**
* @param Parameters $attributes
*
* @return list<T&Proxy>
* @phpstan-return list<Proxy<T>>
* @return list<Proxy<T>>
*/
final public static function findBy(array $attributes): array
{
Expand All @@ -182,8 +170,7 @@ final public static function findBy(array $attributes): array
/**
* @param Parameters $attributes
*
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public static function random(array $attributes = []): object
{
Expand All @@ -193,8 +180,7 @@ final public static function random(array $attributes = []): object
/**
* @param Parameters $attributes
*
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public static function randomOrCreate(array $attributes = []): object
{
Expand All @@ -208,8 +194,7 @@ final public static function randomOrCreate(array $attributes = []): object
/**
* @param Parameters $attributes
*
* @return list<T&Proxy>
* @phpstan-return list<Proxy<T>>
* @return list<Proxy<T>>
*/
final public static function randomSet(int $number, array $attributes = []): array
{
Expand All @@ -219,8 +204,7 @@ final public static function randomSet(int $number, array $attributes = []): arr
/**
* @param Parameters $attributes
*
* @return list<T&Proxy>
* @phpstan-return list<Proxy<T>>
* @return list<Proxy<T>>
*/
final public static function randomRange(int $min, int $max, array $attributes = []): array
{
Expand All @@ -241,16 +225,15 @@ final public static function truncate(): void
}

/**
* @phpstan-return RepositoryProxy<T>
* @return RepositoryProxy<T>
*/
final public static function repository(): RepositoryProxy
{
return static::persistenceManager()->repositoryFor(static::class());
}

/**
* @return T&Proxy
* @phpstan-return Proxy<T>
* @return Proxy<T>
*/
final public function create(array|callable $attributes = []): object
{
Expand Down
Loading

0 comments on commit 137c23c

Please sign in to comment.