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