| Conditions | 8 |
| Paths | 63 |
| Total Lines | 57 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 28 | public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null) |
||
| 29 | { |
||
| 30 | $securityHandler = $admin->getSecurityHandler(); |
||
| 31 | if (!$securityHandler instanceof AclSecurityHandlerInterface) { |
||
| 32 | $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>'); |
||
| 33 | |||
| 34 | return; |
||
| 35 | } |
||
| 36 | |||
| 37 | $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode())); |
||
| 38 | $objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner'; |
||
| 39 | |||
| 40 | /** @var DocumentManager $om */ |
||
| 41 | $om = $admin->getModelManager()->getDocumentManager(); |
||
| 42 | $qb = $om->createQueryBuilder($admin->getClass()); |
||
| 43 | |||
| 44 | $count = 0; |
||
| 45 | $countUpdated = 0; |
||
| 46 | $countAdded = 0; |
||
| 47 | |||
| 48 | try { |
||
| 49 | $batchSize = 20; |
||
| 50 | $batchSizeOutput = 200; |
||
| 51 | $objectIds = array(); |
||
| 52 | |||
| 53 | foreach ($qb->getQuery()->iterate() as $row) { |
||
| 54 | $objectIds[] = ObjectIdentity::fromDomainObject($row); |
||
| 55 | $objectIdIterator = new \ArrayIterator($objectIds); |
||
| 56 | |||
| 57 | // detach from Doctrine, so that it can be Garbage-Collected immediately |
||
| 58 | $om->detach($row); |
||
| 59 | |||
| 60 | ++$count; |
||
| 61 | |||
| 62 | if (($count % $batchSize) == 0) { |
||
| 63 | list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity); |
||
| 64 | $countAdded += $batchAdded; |
||
| 65 | $countUpdated += $batchUpdated; |
||
| 66 | $objectIds = array(); |
||
| 67 | } |
||
| 68 | |||
| 69 | if (($count % $batchSizeOutput) == 0) { |
||
| 70 | $output->writeln(sprintf(' - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated)); |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | if (count($objectIds) > 0) { |
||
| 75 | list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity); |
||
|
|
|||
| 76 | $countAdded += $batchAdded; |
||
| 77 | $countUpdated += $batchUpdated; |
||
| 78 | } |
||
| 79 | } catch (\BadMethodCallException $e) { |
||
| 80 | throw new ModelManagerException('', 0, $e); |
||
| 81 | } |
||
| 82 | |||
| 83 | $output->writeln(sprintf(' - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated)); |
||
| 84 | } |
||
| 85 | } |
||
| 86 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: