| Conditions | 13 |
| Paths | 11 |
| Total Lines | 35 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 2 | Features | 1 |
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 apply() |
||
| 105 | { |
||
| 106 | $value = $this->getValue(); |
||
| 107 | if (null === $value || '' === $value) { |
||
| 108 | return; |
||
| 109 | } |
||
| 110 | if ($func = $this->config->getFilteringFunc()) { |
||
| 111 | $func($value, $this->grid->getConfig()->getDataProvider()); |
||
| 112 | return; |
||
| 113 | } |
||
| 114 | $operator = $this->config->getOperator(); |
||
| 115 | if ($operator === FilterConfig::OPERATOR_LIKE || $operator === FilterConfig::OPERATOR_LIKE_R) { |
||
| 116 | // Search for non-escaped wildcards |
||
| 117 | $found = false; |
||
| 118 | for ($i = 0; $i < mb_strlen($value); $i++) { |
||
| 119 | if (in_array(mb_substr($value, $i, 1), ['%', '_']) && $i > 0 && mb_substr($value, $i - 1, 1) != '\\') { |
||
| 120 | $found = true; |
||
| 121 | break; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | // If none are found, insert wildcards to improve user experience |
||
| 125 | if (!$found) { |
||
| 126 | if ($operator === FilterConfig::OPERATOR_LIKE) { |
||
| 127 | $value = "%$value%"; |
||
| 128 | } else if ($operator === FilterConfig::OPERATOR_LIKE_R) { |
||
| 129 | $value .= '%'; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | $this->grid->getConfig()->getDataProvider()->filter( |
||
| 134 | $this->config->getName(), |
||
| 135 | $this->config->getOperator(), |
||
| 136 | $value |
||
| 137 | ); |
||
| 138 | } |
||
| 139 | } |
||
| 140 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: