| Conditions | 19 |
| Paths | 35 |
| Total Lines | 79 |
| Code Lines | 41 |
| Lines | 24 |
| Ratio | 30.38 % |
| Changes | 4 | ||
| Bugs | 1 | 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 |
||
| 15 | public function checked($value = null, $oKey = null, $oName = null) { |
||
| 16 | |||
| 17 | if (is_null($value)) { |
||
| 18 | return $this->getChecked(); |
||
| 19 | } |
||
| 20 | |||
| 21 | //Checkboxy $this->value powinny mieć wyłącznie jako "string" |
||
| 22 | if (is_array($this->value)) { |
||
| 23 | $this->value = $this->value[0]; |
||
|
|
|||
| 24 | } |
||
| 25 | |||
| 26 | $this->checked = false; |
||
| 27 | |||
| 28 | if (is_array($value)) { |
||
| 29 | if (isAssoc($value)) { |
||
| 30 | //W przypadku tablicy asocjacyjnej ($key=>$val) jeśli $val jest typu bool to znaczy że $key == $this->value |
||
| 31 | if (is_bool(array_values($value)[0])) { |
||
| 32 | if (array_key_exists($this->value, $value)) { |
||
| 33 | $this->checked = $value[$this->value]; |
||
| 34 | return $this; |
||
| 35 | } |
||
| 36 | } else { |
||
| 37 | //W przeciwnym przypdaku uznajemy że tablica przechowuje pary klucz => wartosc odpowiadające $this->name => $this->value |
||
| 38 | if (array_key_exists($this->name, $value)) { |
||
| 39 | $this->checked = $value[$this->name] == $this->value; |
||
| 40 | return $this; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | } else { |
||
| 44 | $this->checked = in_array($this->value, $value); |
||
| 45 | return $this; |
||
| 46 | } |
||
| 47 | |||
| 48 | return $this; |
||
| 49 | } |
||
| 50 | |||
| 51 | //Jeżeli jest obiektem to obowiązkowo należy podać drugi parametr jakim jest nazwa pola |
||
| 52 | if (is_object($value) && $oKey && $oName) { |
||
| 53 | foreach($value as $v) |
||
| 54 | { |
||
| 55 | View Code Duplication | if (property_exists($v, $oKey)) |
|
| 56 | { |
||
| 57 | $prop = $v->$oKey; |
||
| 58 | if (is_bool($prop)) |
||
| 59 | { |
||
| 60 | $this->checked = $prop; |
||
| 61 | } else |
||
| 62 | { |
||
| 63 | $this->checked = $prop == $this->value; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } |
||
| 67 | return $this; |
||
| 68 | } |
||
| 69 | |||
| 70 | |||
| 71 | View Code Duplication | if (is_object($value) && $oKey) { |
|
| 72 | if (property_exists($value, $oKey)) |
||
| 73 | { |
||
| 74 | $prop = $value->$oKey; |
||
| 75 | if (is_bool($prop)) |
||
| 76 | { |
||
| 77 | $this->checked = $prop; |
||
| 78 | } else |
||
| 79 | { |
||
| 80 | $this->checked = $prop == $this->value; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | if (is_bool($value)) { |
||
| 86 | $this->checked = $value; |
||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->checked = $this->value == $value; |
||
| 91 | |||
| 92 | return $this; |
||
| 93 | } |
||
| 94 | |||
| 125 | } |
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: