Conditions | 8 |
Paths | 63 |
Total Lines | 63 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 2 | 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 |
||
27 | public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null) |
||
28 | { |
||
29 | $securityHandler = $admin->getSecurityHandler(); |
||
30 | if (!$securityHandler instanceof AclSecurityHandlerInterface) { |
||
31 | $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>'); |
||
32 | |||
33 | return; |
||
34 | } |
||
35 | |||
36 | $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode())); |
||
37 | $objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner'; |
||
38 | |||
39 | $em = $admin->getModelManager()->getEntityManager($admin->getClass()); |
||
40 | $qb = $em->createQueryBuilder(); |
||
41 | $qb->select('o')->from($admin->getClass(), 'o'); |
||
42 | |||
43 | $count = 0; |
||
44 | $countUpdated = 0; |
||
45 | $countAdded = 0; |
||
46 | |||
47 | try { |
||
48 | $batchSize = 20; |
||
49 | $batchSizeOutput = 200; |
||
50 | $objectIds = array(); |
||
51 | |||
52 | foreach ($qb->getQuery()->iterate() as $row) { |
||
53 | $objectIds[] = ObjectIdentity::fromDomainObject($row[0]); |
||
54 | $objectIdIterator = new \ArrayIterator($objectIds); |
||
55 | |||
56 | // detach from Doctrine, so that it can be Garbage-Collected immediately |
||
57 | $em->detach($row[0]); |
||
58 | |||
59 | ++$count; |
||
60 | |||
61 | if (($count % $batchSize) == 0) { |
||
62 | list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity); |
||
63 | $countAdded += $batchAdded; |
||
64 | $countUpdated += $batchUpdated; |
||
65 | $objectIds = array(); |
||
66 | } |
||
67 | |||
68 | if (($count % $batchSizeOutput) == 0) { |
||
69 | $output->writeln(sprintf(' - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated)); |
||
70 | } |
||
71 | } |
||
72 | |||
73 | if (count($objectIds) > 0) { |
||
74 | list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $objectIdIterator, $securityIdentity); |
||
|
|||
75 | $countAdded += $batchAdded; |
||
76 | $countUpdated += $batchUpdated; |
||
77 | } |
||
78 | } catch (\PDOException $e) { |
||
79 | throw new ModelManagerException('', 0, $e); |
||
80 | } |
||
81 | |||
82 | $output->writeln(sprintf( |
||
83 | ' - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)', |
||
84 | $objectOwnersMsg, |
||
85 | $count, |
||
86 | $countAdded, |
||
87 | $countUpdated |
||
88 | )); |
||
89 | } |
||
90 | } |
||
91 |
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: