| Conditions | 8 |
| Paths | 9 |
| Total Lines | 94 |
| Code Lines | 64 |
| 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 |
||
| 104 | public function subscribeEvents( |
||
| 105 | EntityMapperInterface $mapper, |
||
| 106 | int $type = self::ALL, |
||
| 107 | array $ignoreFields = [] |
||
| 108 | ): void { |
||
| 109 | if ($this->authentication->isLogged() === false) { |
||
| 110 | return; |
||
| 111 | } |
||
| 112 | |||
| 113 | $auditor = $this->auditor; |
||
| 114 | $ignore = $this->ignore; |
||
| 115 | $fieldIgnores = [ |
||
| 116 | ...$ignoreFields, |
||
| 117 | ...['password', 'created_at', 'updated_at'], |
||
| 118 | ]; |
||
| 119 | |||
| 120 | if ($type & self::CREATE) { |
||
| 121 | $mapper->on('save', function ( |
||
| 122 | Entity $entity, |
||
| 123 | DataMapper $dm |
||
|
|
|||
| 124 | ) use ( |
||
| 125 | $auditor, |
||
| 126 | $ignore, |
||
| 127 | $fieldIgnores |
||
| 128 | ) { |
||
| 129 | if ($ignore) { |
||
| 130 | return; |
||
| 131 | } |
||
| 132 | |||
| 133 | $data = $entity->jsonSerialize(); |
||
| 134 | $entityData = Arr::except($data, $fieldIgnores); |
||
| 135 | $className = Php::getShortClassName($entity); |
||
| 136 | |||
| 137 | $auditor->setDetail(sprintf( |
||
| 138 | 'Create of "%s" %s', |
||
| 139 | $className, |
||
| 140 | Str::stringify($entityData) |
||
| 141 | )) |
||
| 142 | ->setEvent(EventType::CREATE) |
||
| 143 | ->save(); |
||
| 144 | }); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($type & self::UPDATE) { |
||
| 148 | $mapper->on('update', function ( |
||
| 149 | Entity $entity, |
||
| 150 | DataMapper $dm |
||
| 151 | ) use ( |
||
| 152 | $auditor, |
||
| 153 | $ignore, |
||
| 154 | $fieldIgnores |
||
| 155 | ) { |
||
| 156 | if ($ignore) { |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | $data = $entity->jsonSerialize(); |
||
| 161 | $entityData = Arr::except($data, $fieldIgnores); |
||
| 162 | $className = Php::getShortClassName($entity); |
||
| 163 | |||
| 164 | $auditor->setDetail(sprintf( |
||
| 165 | 'Update of %s %s', |
||
| 166 | $className, |
||
| 167 | Str::stringify($entityData) |
||
| 168 | )) |
||
| 169 | ->setEvent(EventType::UPDATE) |
||
| 170 | ->save(); |
||
| 171 | }); |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($type & self::DELETE) { |
||
| 175 | $mapper->on('delete', function ( |
||
| 176 | Entity $entity, |
||
| 177 | DataMapper $dm |
||
| 178 | ) use ( |
||
| 179 | $auditor, |
||
| 180 | $ignore, |
||
| 181 | $fieldIgnores |
||
| 182 | ) { |
||
| 183 | if ($ignore) { |
||
| 184 | return; |
||
| 185 | } |
||
| 186 | |||
| 187 | $data = $entity->jsonSerialize(); |
||
| 188 | $entityData = Arr::except($data, $fieldIgnores); |
||
| 189 | $className = Php::getShortClassName($entity); |
||
| 190 | |||
| 191 | $auditor->setDetail(sprintf( |
||
| 192 | 'Delete of %s %s', |
||
| 193 | $className, |
||
| 194 | Str::stringify($entityData) |
||
| 195 | )) |
||
| 196 | ->setEvent(EventType::DELETE) |
||
| 197 | ->save(); |
||
| 198 | }); |
||
| 315 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.