Skip to content

Commit

Permalink
update phpstan: fix generics & add more safety checks
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Mar 8, 2021
1 parent 52fdab0 commit ab9cdcf
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"marc-mabe/php-enum": "~3.0",
"mockery/mockery": "~1.2",
"phpstan/extension-installer": "1.1.0",
"phpstan/phpstan": "0.12.71",
"phpstan/phpstan": "0.12.81",
"phpstan/phpstan-deprecation-rules": "0.12.6",
"phpstan/phpstan-nette": "0.12.14",
"phpstan/phpstan-mockery": "0.12.12",
Expand Down
5 changes: 3 additions & 2 deletions src/Bridges/NetteDI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nextras\Orm\Mapper\Dbal\DbalMapperCoordinator;
use Nextras\Orm\Model\MetadataStorage;
use Nextras\Orm\Model\Model;
use function is_subclass_of;
use function method_exists;


Expand Down Expand Up @@ -40,10 +41,10 @@ public function loadConfiguration()
$this->modelClass = $config['model'];

$repositoryFinderClass = $config['repositoryFinder'];
$this->repositoryFinder = new $repositoryFinderClass($this->modelClass, $this->builder, $this);
if (!$this->repositoryFinder instanceof IRepositoryFinder) {
if (!is_subclass_of($repositoryFinderClass, IRepositoryFinder::class)) {
throw new InvalidStateException('Repository finder does not implement Nextras\Orm\Bridges\NetteDI\IRepositoryFinder interface.');
}
$this->repositoryFinder = new $repositoryFinderClass($this->modelClass, $this->builder, $this);

$repositories = $this->repositoryFinder->loadConfiguration();

Expand Down
7 changes: 4 additions & 3 deletions src/Entity/Reflection/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Nette\SmartObject;
use Nextras\Orm\Entity\IProperty;
use Nextras\Orm\Exception\InvalidStateException;
use function is_subclass_of;


class PropertyMetadata
Expand Down Expand Up @@ -69,10 +70,10 @@ class PropertyMetadata
public function getWrapperPrototype(): IProperty
{
if ($this->wrapperPrototype === null) {
if ($this->wrapper === null) {
throw new InvalidStateException();
}
$class = $this->wrapper;
if ($class === null || !is_subclass_of($class, IProperty::class)) {
throw new InvalidStateException('Wrapper class has to implement ' . IProperty::class . ' interface.');
}
$this->wrapperPrototype = new $class($this);
}
return $this->wrapperPrototype;
Expand Down
3 changes: 2 additions & 1 deletion src/Mapper/Dbal/Conventions/Conventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ protected function getDefaultMappings(): array

/** @phpstan-var list<array{EntityMetadata, list<string>}> $toProcess */
$toProcess = [[$this->entityMetadata, []]];
while (([$metadata, $tokens] = array_shift($toProcess)) !== null) {
while (($entry = array_shift($toProcess)) !== null) {
[$metadata, $tokens] = $entry;
foreach ($metadata->getProperties() as $property) {
if ($property->wrapper !== EmbeddableContainer::class) {
continue;
Expand Down
5 changes: 3 additions & 2 deletions src/Model/IModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public function hasRepository(string $className): bool;

/**
* Returns repository by repository class.
* @template T of IRepository
* @template E of IEntity
* @template T of IRepository<E>
* @phpstan-param class-string<T> $className
* @phpstan-return T
*/
Expand All @@ -42,7 +43,7 @@ public function getRepository(string $className): IRepository;
/**
* Returns repository associated for entity type.
* @param IEntity|string $entity
* @phpstan-template E of IEntity
* @template E of IEntity
* @phpstan-param E|class-string<E> $entity
* @phpstan-return IRepository<E>
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Model/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function hasRepository(string $className): bool

/**
* Returns repository by repository class.
* @template T of IRepository
* @template E of IEntity
* @template T of IRepository<E>
* @phpstan-param class-string<T> $className
* @phpstan-return T
*/
Expand Down
4 changes: 3 additions & 1 deletion src/Repository/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ protected function createCollectionFunction(string $name)
}

if (isset($knownFunctions[$name])) {
return new $name();
/** @var IQueryBuilderFunction|IArrayFunction $function */
$function = new $name();
return $function;
} else {
throw new NotImplementedException('Override ' . get_class($this) . '::createCollectionFunction() to return an instance of ' . $name . ' collection function.');
}
Expand Down

0 comments on commit ab9cdcf

Please sign in to comment.