| 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 |
||
| 72 | protected function doDeleteAction() |
||
| 73 | { |
||
| 74 | if (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'District', 'delete')) { |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $sessionToken = $this->getSession()->getCsrfToken(); |
||
| 79 | $actionToken = filter_input(INPUT_POST, 'token'); |
||
| 80 | |||
| 81 | if (! $actionToken || ! $sessionToken->isValid($actionToken)) { |
||
| 82 | return; |
||
| 83 | } |
||
| 84 | |||
| 85 | $districts = $this->districts; |
||
| 86 | |||
| 87 | if (! $districts || ! is_array($districts)) { |
||
|
|
|||
| 88 | return; |
||
| 89 | } |
||
| 90 | |||
| 91 | $deletedDistrictsCount = 0; |
||
| 92 | |||
| 93 | foreach ($districts as $district) { |
||
| 94 | if ($this->getAcl()->canDeleteEntity($this->getAuthenticatedUser(), $district)) { |
||
| 95 | $donorsCount = $this->getDonorRepository()->countBy(['district' => $districts]); |
||
| 96 | |||
| 97 | if ($donorsCount > 0) { |
||
| 98 | Notices::addNotice('linked_donors_exists', sprintf(__('At first, delete any linked donors with district "%s".'), $district->get('id'))); |
||
| 99 | return; |
||
| 100 | } |
||
| 101 | |||
| 102 | $this->getEntityManager()->remove($district); |
||
| 103 | $deletedDistrictsCount++; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->getEntityManager()->flush(); |
||
| 108 | |||
| 109 | EBB\redirect( |
||
| 110 | EBB\addQueryArgs( |
||
| 111 | EBB\getEditDistrictsURL(), |
||
| 112 | ['flag-deleted' => $deletedDistrictsCount] |
||
| 113 | ) |
||
| 117 |
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.