| Conditions | 12 |
| Paths | 60 |
| Total Lines | 44 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 93 | function updatePermissionOptions($options, $user, $item) |
||
| 94 | { |
||
| 95 | $permissions = []; |
||
| 96 | |||
| 97 | if ($user->role) { |
||
| 98 | $permissions = $user->role->permissions()->module()->pluck('name','permissionable_type')->all(); |
||
| 99 | if (empty($permissions)) { |
||
| 100 | if ($user->role->permissions()->global()->where('name', 'manage-modules')->first()){ |
||
| 101 | $permissions[get_class($item)] = 'manage-item'; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // looking for group permissions belonging to the user |
||
| 107 | foreach($user->publishedGroups as $group) { |
||
| 108 | if (($permission=$group->permissions()->ofItem($item)->first())!= null) { |
||
| 109 | if (isset($permissions[get_class($item)])) { |
||
| 110 | $scopes = Permission::available(Permission::SCOPE_ITEM); |
||
| 111 | $previous = array_search($permissions[get_class($item)], $scopes); |
||
| 112 | $current = array_search($permission->name, $scopes); |
||
| 113 | |||
| 114 | // check permission level |
||
| 115 | if ($current > $previous) { |
||
| 116 | $permissions[get_class($item)] = $permission->name; |
||
| 117 | } |
||
| 118 | |||
| 119 | } else { |
||
| 120 | $permissions[get_class($item)] = $permission->name; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if (isset($permissions[get_class($item)])) { |
||
| 126 | $globalPermission = str_replace('-module', '-item', $permissions[get_class($item)]); |
||
| 127 | foreach($options as &$option) { |
||
| 128 | if ($option['value'] != $globalPermission || $globalPermission=='manage-item') { |
||
| 129 | $option['disabled'] = true; |
||
| 130 | } else { |
||
| 131 | break; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return $options; |
||
| 137 | } |
||
| 179 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: