Skip to content

Commit

Permalink
fixes for latest PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 4, 2023
1 parent 7de9131 commit 970d4e2
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 45 deletions.
2 changes: 0 additions & 2 deletions src/Collection/DbalCollection.php
Expand Up @@ -139,8 +139,6 @@ public function orderBy($expression, string $direction = ICollection::ASC): ICol
];
}
} else {
/** @phpstan-var string|list<mixed> $expression */
$expression = $expression; // no-op for PHPStan
$collection->ordering[] = [
$helper->processPropertyExpr($collection->queryBuilder, $expression),
$direction,
Expand Down
3 changes: 1 addition & 2 deletions src/Collection/Functions/CompareEqualsFunction.php
Expand Up @@ -40,9 +40,8 @@ protected function evaluateInDb(DbalExpressionResult $expression, $value, string
$columns[] = $column . $modifiers[$i];
}
$value = array_map(function ($value) use ($columns): array {
/** @var array<string, string>|false $combined */
$combined = array_combine($columns, $value);
if ($combined === false) {
if ($combined === false) { // @phpstan-ignore-line
$pn = count($columns);
$vn = count($value);
throw new InvalidArgumentException("Number of values ($vn) does not match number of properties ($pn).");
Expand Down
3 changes: 1 addition & 2 deletions src/Collection/Functions/CompareNotEqualsFunction.php
Expand Up @@ -40,9 +40,8 @@ protected function evaluateInDb(DbalExpressionResult $expression, $value, string
$columns[] = $column . $modifiers[$i];
}
$value = array_map(function ($value) use ($columns): array {
/** @var array<string, string>|false $combined */
$combined = array_combine($columns, $value);
if ($combined === false) {
if ($combined === false) { // @phpstan-ignore-line
$pn = count($columns);
$vn = count($value);
throw new InvalidArgumentException("Number of values ($vn) does not match number of properties ($pn).");
Expand Down
4 changes: 2 additions & 2 deletions src/Collection/HasManyCollection.php
Expand Up @@ -47,7 +47,7 @@ class HasManyCollection implements ICollection

/**
* @var callable A callback returning a list entities to add & remove.
* @phpstan-var callable(): array{array<string, E>, array<string, E>}
* @phpstan-var callable(): array{array<array-key, E>, array<array-key, E>}
*/
private $diffCallback;

Expand All @@ -58,7 +58,7 @@ class HasManyCollection implements ICollection
/**
* @phpstan-param IRepository<E> $repository
* @phpstan-param ICollection<E> $innerCollection
* @phpstan-param callable():array{array<string, E>, array<string, E>} $diffCallback
* @phpstan-param callable():array{array<array-key, E>, array<array-key, E>} $diffCallback
*/
public function __construct(
IRepository $repository,
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/AbstractEntity.php
Expand Up @@ -399,7 +399,7 @@ public function onAfterRemove(): void
* @param mixed $value
* @return mixed
*/
private function setterPrimaryProxy($value, PropertyMetadata $metadata)
private function setterPrimaryProxy($value, PropertyMetadata $metadata) // @phpstan-ignore-line
{
$keys = $this->metadata->getPrimaryKey();
if (!$metadata->isVirtual) {
Expand Down Expand Up @@ -429,7 +429,7 @@ private function setterPrimaryProxy($value, PropertyMetadata $metadata)
* @param mixed $value
* @return mixed
*/
private function getterPrimaryProxy($value, PropertyMetadata $metadata)
private function getterPrimaryProxy($value, PropertyMetadata $metadata) // @phpstan-ignore-line
{
if ($this->persistedId !== null) {
return $this->persistedId;
Expand Down
5 changes: 1 addition & 4 deletions src/Entity/Reflection/ModifierParser.php
Expand Up @@ -217,10 +217,7 @@ private function processKeyword(string $value, ReflectionClass $reflectionClass)
} elseif (strcasecmp($value, 'null') === 0) {
return null;
} elseif (is_numeric($value)) {
// hack for phpstan
/** @var int $val */
$val = $value;
return $val * 1;
return $value * 1;
} elseif (preg_match('#^[a-z0-9_\\\\]+::[a-z0-9_]*(\\*)?$#i', $value) === 1) {
[$className, $const] = explode('::', $value, 2);
if ($className === 'self' || $className === 'static') {
Expand Down
19 changes: 10 additions & 9 deletions src/Relationships/HasMany.php
Expand Up @@ -50,19 +50,19 @@ abstract class HasMany implements IRelationshipCollection

/**
* @var IEntity[]
* @phpstan-var array<int, E>
* @phpstan-var array<array-key, E>
*/
protected $toAdd = [];

/**
* @var IEntity[]
* @phpstan-var array<int,E>
* @phpstan-var array<array-key, E>
*/
protected $toRemove = [];

/**
* @var IEntity[]
* @phpstan-var array<int, E>
* @phpstan-var array<array-key, E>
*/
protected $tracked = [];

Expand Down Expand Up @@ -348,14 +348,15 @@ protected function getCollection(bool $forceNew = false): ICollection
}

if (count($this->toAdd) > 0 || count($this->toRemove) > 0) {
/** @phpstan-var callable():array{array<string, E>, array<string, E>} $diffCb */
$diffCb = function (): array {
return [$this->toAdd, $this->toRemove];
};

$collection = $collection->resetOrderBy();
/** @var ICollection<E> $collection */
$collection = new HasManyCollection($this->getTargetRepository(), $collection, $diffCb);
$collection = new HasManyCollection(
$this->getTargetRepository(),
$collection,
function (): array {
return [$this->toAdd, $this->toRemove];
}
);
$collection = $this->applyDefaultOrder($collection);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Relationships/IRelationshipCollection.php
Expand Up @@ -88,7 +88,7 @@ public function trackEntity(IEntity $entity): void;
/**
* Returns IEntity for persistence.
* @return IEntity[]
* @phpstan-return array<int, E>
* @phpstan-return array<array-key, E>
* @ignore
* @internal
*/
Expand Down
21 changes: 11 additions & 10 deletions src/Relationships/ManyHasMany.php
Expand Up @@ -66,22 +66,23 @@ protected function modify(): void

protected function createCollection(): ICollection
{
/** @phpstan-var callable(Traversable<mixed,E>):void $subscribeCb */
$subscribeCb = function (Traversable $entities): void {
$mapper = $this->parent->getRepository()->getMapper();

/** @var ICollection<E> $collection */
$collection = $this->getTargetRepository()->getMapper()->createCollectionManyHasMany($mapper, $this->metadata);
$collection = $collection->setRelationshipParent($this->parent);
$collection->subscribeOnEntityFetch(function (Traversable $entities): void {
if ($this->metadataRelationship->property === null) {
return;
}
foreach ($entities as $entity) {
$entity->getProperty($this->metadataRelationship->property)->trackEntity($this->parent);
assert($this->metadataRelationship->property !== null);
$property = $entity->getProperty($this->metadataRelationship->property);
assert($property instanceof HasMany);
$property->trackEntity($this->parent);
$this->trackEntity($entity);
}
};
$mapper = $this->parent->getRepository()->getMapper();

/** @var ICollection<E> $collection */
$collection = $this->getTargetRepository()->getMapper()->createCollectionManyHasMany($mapper, $this->metadata);
$collection = $collection->setRelationshipParent($this->parent);
$collection->subscribeOnEntityFetch($subscribeCb);
});
return $this->applyDefaultOrder($collection);
}

Expand Down
13 changes: 5 additions & 8 deletions src/Relationships/OneHasMany.php
Expand Up @@ -57,17 +57,14 @@ protected function modify(): void

protected function createCollection(): ICollection
{
/** @phpstan-var callable(\Traversable<mixed,E>):void $subscribeCb */
$subscribeCb = function ($entities): void {
foreach ($entities as $entity) {
$this->trackEntity($entity);
}
};

/** @var ICollection<E> $collection */
$collection = $this->getTargetRepository()->getMapper()->createCollectionOneHasMany($this->metadata);
$collection = $collection->setRelationshipParent($this->parent);
$collection->subscribeOnEntityFetch($subscribeCb);
$collection->subscribeOnEntityFetch(function ($entities): void {
foreach ($entities as $entity) {
$this->trackEntity($entity);
}
});
return $this->applyDefaultOrder($collection);
}

Expand Down
7 changes: 4 additions & 3 deletions tests/inc/phpstan/AssertTypeSpecifierExtension.php
Expand Up @@ -11,9 +11,9 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\StaticMethodTypeSpecifyingExtension;
use Tester\Assert;
use function count;


class AssertTypeSpecifyingExtension implements StaticMethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
Expand Down Expand Up @@ -66,13 +66,14 @@ public function specifyTypes(
$class = $node->getArgs()[0];

$classType = $scope->getType($class->value);
if (!$classType instanceof ConstantStringType) {
$value = $classType->getConstantStrings();
if (count($value) !== 1) {
return new \PHPStan\Analyser\SpecifiedTypes();
}

$expression = new \PhpParser\Node\Expr\Instanceof_(
$expr->value,
new \PhpParser\Node\Name($classType->getValue())
new \PhpParser\Node\Name($value[0]->getValue())
);
} else {
throw new ShouldNotHappenException();
Expand Down

0 comments on commit 970d4e2

Please sign in to comment.