Fixed phpstan analysis issues and bad code that showed up with phpstan 2.0

This commit is contained in:
Jan Böhmer 2024-12-28 22:31:04 +01:00
parent a273acbecd
commit 946032a101
42 changed files with 98 additions and 85 deletions

View file

@ -23,9 +23,11 @@ declare(strict_types=1);
namespace App\Services\Trees;
use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\Base\AbstractStructuralDBElement;
use App\Repository\AttachmentContainingDBElementRepository;
use App\Repository\DBElementRepository;
use App\Repository\NamedDBElementRepository;
use App\Repository\StructuralDBElementRepository;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
@ -51,7 +53,7 @@ class NodesListBuilder
* Gets a flattened hierarchical tree. Useful for generating option lists.
* In difference to the Repository Function, the results here are cached.
*
* @template T of AbstractDBElement
* @template T of AbstractNamedDBElement
*
* @param string $class_name the class name of the entity you want to retrieve
* @phpstan-param class-string<T> $class_name
@ -69,7 +71,7 @@ class NodesListBuilder
$ids = $this->getFlattenedIDs($class_name, $parent);
//Retrieve the elements from the IDs, the order is the same as in the $ids array
/** @var DBElementRepository $repo */
/** @var NamedDBElementRepository<T> $repo */
$repo = $this->em->getRepository($class_name);
if ($repo instanceof AttachmentContainingDBElementRepository) {
@ -81,7 +83,9 @@ class NodesListBuilder
/**
* This functions returns the (cached) list of the IDs of the elements for the flattened tree.
* @template T of AbstractNamedDBElement
* @param string $class_name
* @phpstan-param class-string<T> $class_name
* @param AbstractStructuralDBElement|null $parent
* @return int[]
*/
@ -96,10 +100,11 @@ class NodesListBuilder
// Invalidate when groups, an element with the class or the user changes
$item->tag(['groups', 'tree_list', $this->keyGenerator->generateKey(), $secure_class_name]);
/** @var StructuralDBElementRepository $repo */
/** @var NamedDBElementRepository<T> $repo */
$repo = $this->em->getRepository($class_name);
return array_map(static fn(AbstractDBElement $element) => $element->getID(), $repo->getFlatList($parent));
return array_map(static fn(AbstractDBElement $element) => $element->getID(),
$repo instanceof AbstractStructuralDBElement ? $repo->getFlatList($parent) : $repo->getFlatList());
});
}

View file

@ -33,6 +33,7 @@ use App\Entity\Parts\Supplier;
use App\Entity\ProjectSystem\Project;
use App\Helpers\Trees\TreeViewNode;
use App\Helpers\Trees\TreeViewNodeIterator;
use App\Repository\NamedDBElementRepository;
use App\Repository\StructuralDBElementRepository;
use App\Services\Cache\ElementCacheTagGenerator;
use App\Services\Cache\UserCacheKeyGenerator;
@ -219,6 +220,7 @@ class TreeViewGenerator
* The treeview is generic, that means the href are null and ID values are set.
*
* @param string $class The class for which the tree should be generated
* @phpstan-param class-string<AbstractNamedDBElement> $class
* @param AbstractStructuralDBElement|null $parent the parent the root elements should have
*
* @return TreeViewNode[]
@ -232,12 +234,12 @@ class TreeViewGenerator
throw new InvalidArgumentException('$parent must be of the type $class!');
}
/** @var StructuralDBElementRepository $repo */
/** @var NamedDBElementRepository<AbstractNamedDBElement> $repo */
$repo = $this->em->getRepository($class);
//If we just want a part of a tree, don't cache it
if ($parent instanceof AbstractStructuralDBElement) {
return $repo->getGenericNodeTree($parent);
return $repo->getGenericNodeTree($parent); //@phpstan-ignore-line PHPstan does not seem to recognize, that we have a StructuralDBElementRepository here, which have 1 argument
}
$secure_class_name = $this->tagGenerator->getElementTypeCacheTag($class);
@ -246,7 +248,7 @@ class TreeViewGenerator
return $this->cache->get($key, function (ItemInterface $item) use ($repo, $parent, $secure_class_name) {
// Invalidate when groups, an element with the class or the user changes
$item->tag(['groups', 'tree_treeview', $this->keyGenerator->generateKey(), $secure_class_name]);
return $repo->getGenericNodeTree($parent);
return $repo->getGenericNodeTree($parent); //@phpstan-ignore-line
});
}
}