Conditions | 16 |
Paths | 772 |
Total Lines | 61 |
Code Lines | 35 |
Lines | 0 |
Ratio | 0 % |
Changes | 8 | ||
Bugs | 3 | Features | 3 |
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 |
||
61 | public function save($data) |
||
62 | { |
||
63 | if (!empty($data['matricule'])) { |
||
64 | $pompier = $this->pompier_repository->find($data['matricule']); |
||
65 | } |
||
66 | |||
67 | if (empty($pompier)) { |
||
68 | $centre = $this->centre_repository->find($data['centre']); |
||
69 | |||
70 | if (empty($centre)) { |
||
71 | return; |
||
72 | } |
||
73 | |||
74 | switch ($data['type']) { |
||
75 | case 'pompier' : |
||
76 | $pompier = new Pompier($data['name'], $data['matricule'], $centre); |
||
77 | break; |
||
78 | case 'specialiste' : |
||
79 | $pompier = new Pompier\SpecialistePompier($data['name'], $data['matricule'], $centre); |
||
80 | break; |
||
81 | default: |
||
82 | throw new InvalidPompierException(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | if (!empty($data['centre'])) { |
||
87 | $pompier->setCentre($this->centre_repository->find($data['centre'])); |
||
88 | } |
||
89 | |||
90 | if (!empty($data['name'])) { |
||
91 | $pompier->setName($data['name']); |
||
92 | } |
||
93 | |||
94 | if (!empty($data['specialites'])) { |
||
95 | $pompier->setSpecialites($data['specialites']); |
||
96 | } |
||
97 | |||
98 | if (!empty($data['phone_number'])) { |
||
99 | $pompier->setPhoneNumber($data['phone_number']); |
||
100 | } |
||
101 | |||
102 | if (!empty($data['statut'])) { |
||
103 | $pompier->setStatut(Statut::getByName($data['statut'])); |
||
104 | } |
||
105 | |||
106 | if (array_key_exists('pro', $data)) { |
||
107 | $pompier->setPro($data['pro'] === true); |
||
108 | } |
||
109 | |||
110 | if (!empty($data['coordinates']) && is_array($data['coordinates']) && count($data['coordinates']) >= 2) { |
||
111 | $pompier->setCoordinates(new Coordinates( |
||
112 | $data['coordinates'][0], |
||
113 | $data['coordinates'][1], |
||
114 | array_key_exists(2, $data['coordinates']) ? $data['coordinates'][2] : null |
||
115 | )); |
||
116 | } |
||
117 | |||
118 | $this->pompier_repository->save($pompier); |
||
119 | |||
120 | return $pompier; |
||
121 | } |
||
122 | |||
143 |
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: