Conditions | 10 |
Paths | 12 |
Total Lines | 41 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
68 | public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []) |
||
69 | { |
||
70 | /** @var EntityManagerInterface $manager */ |
||
71 | $manager = $this->managerRegistry->getManagerForClass($resourceClass); |
||
72 | |||
73 | if ((\is_int($id) || \is_string($id)) && !($context[IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER] ?? false)) { |
||
74 | $id = $this->normalizeIdentifiers($id, $manager, $resourceClass); |
||
75 | } |
||
76 | if (!\is_array($id)) { |
||
77 | throw new \InvalidArgumentException(sprintf( |
||
78 | '$id must be array when "%s" key is set to true in the $context', |
||
79 | IdentifierConverterInterface::HAS_IDENTIFIER_CONVERTER |
||
80 | )); |
||
81 | } |
||
82 | $identifiers = $id; |
||
83 | |||
84 | $fetchData = $context['fetch_data'] ?? true; |
||
85 | if (!$fetchData) { |
||
86 | return $manager->getReference($resourceClass, $identifiers); |
||
87 | } |
||
88 | |||
89 | $repository = $manager->getRepository($resourceClass); |
||
90 | if (!method_exists($repository, 'createQueryBuilder')) { |
||
91 | throw new RuntimeException('The repository class must have a "createQueryBuilder" method.'); |
||
92 | } |
||
93 | |||
94 | $queryBuilder = $repository->createQueryBuilder('o'); |
||
95 | $queryNameGenerator = new QueryNameGenerator(); |
||
96 | $doctrineClassMetadata = $manager->getClassMetadata($resourceClass); |
||
97 | |||
98 | $this->addWhereForIdentifiers($identifiers, $queryBuilder, $doctrineClassMetadata); |
||
99 | |||
100 | foreach ($this->itemExtensions as $extension) { |
||
101 | $extension->applyToItem($queryBuilder, $queryNameGenerator, $resourceClass, $identifiers, $operationName, $context); |
||
102 | |||
103 | if ($extension instanceof QueryResultItemExtensionInterface && $extension->supportsResult($resourceClass, $operationName, $context)) { |
||
|
|||
104 | return $extension->getResult($queryBuilder, $resourceClass, $operationName, $context); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | return $queryBuilder->getQuery()->getOneOrNullResult(); |
||
109 | } |
||
130 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.