Conditions | 11 |
Paths | 48 |
Total Lines | 36 |
Code Lines | 22 |
Lines | 20 |
Ratio | 55.56 % |
Changes | 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 |
||
93 | private function addRoleAndPermissions(Acl &$acl, $role, array $permissions, $resource) |
||
94 | { |
||
95 | if (!$acl->hasRole($role)) { |
||
96 | // Add role |
||
97 | $parentRole = isset($permissions['parent']) ? $permissions['parent'] : null; |
||
98 | $newRole = new Role($role); |
||
99 | $acl->addRole($newRole, $parentRole); |
||
100 | } |
||
101 | |||
102 | if (isset($permissions['superuser']) && $permissions['superuser']) { |
||
103 | $acl->allow($role); |
||
104 | return; |
||
105 | } |
||
106 | |||
107 | View Code Duplication | if (isset($permissions['allowed'])) { |
|
108 | if (is_string($permissions['allowed'])) { |
||
109 | $allowedPermissions = explode(',', $permissions['allowed']); |
||
110 | } else { |
||
111 | $allowedPermissions = $permissions['allowed']; |
||
112 | } |
||
113 | foreach ($allowedPermissions as $allowed) { |
||
114 | $acl->allow($role, $resource, $allowed); |
||
115 | } |
||
116 | } |
||
117 | |||
118 | View Code Duplication | if (isset($permissions['denied'])) { |
|
119 | if (is_string($permissions['denied'])) { |
||
120 | $deniedPermissions = explode(',', $permissions['denied']); |
||
121 | } else { |
||
122 | $deniedPermissions = $permissions['denied']; |
||
123 | } |
||
124 | foreach ($deniedPermissions as $denied) { |
||
125 | $acl->deny($role, $resource, $denied); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: