| Conditions | 10 |
| Paths | 7 |
| Total Lines | 41 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 71 | protected function doDeleteAction() |
||
| 72 | { |
||
| 73 | if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'City', 'delete')) { |
||
| 74 | return; |
||
| 75 | } |
||
| 76 | |||
| 77 | $sessionToken = $this->getSession()->getCsrfToken(); |
||
| 78 | $actionToken = filter_input(INPUT_POST, 'token'); |
||
| 79 | |||
| 80 | if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | $cities = $this->cities; |
||
| 85 | |||
| 86 | if (! $cities || ! is_array($cities)) { |
||
|
|
|||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | $deletedCitiesCount = 0; |
||
| 91 | |||
| 92 | foreach ($cities as $city) { |
||
| 93 | if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $city)) { |
||
| 94 | $districtsCount = $this->getDistrictRepository()->countBy(['city' => $city]); |
||
| 95 | |||
| 96 | if ($districtsCount > 0) { |
||
| 97 | Notices::addNotice('linked_districts_exists', sprintf(__('At first, delete any linked districts with city "%s".'), $city->get('name'))); |
||
| 98 | return; |
||
| 99 | } |
||
| 100 | |||
| 101 | $this->getEntityManager()->remove($city); |
||
| 102 | $deletedCitiesCount++; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | $this->getEntityManager()->flush(); |
||
| 107 | |||
| 108 | EBB\redirect( |
||
| 109 | EBB\addQueryArgs( |
||
| 110 | EBB\getEditCitiesURL(), |
||
| 111 | ['flag-deleted' => $deletedCitiesCount] |
||
| 112 | ) |
||
| 116 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.