Conditions | 10 |
Paths | 19 |
Total Lines | 43 |
Code Lines | 26 |
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 |
||
24 | public function __construct(Connector $client, array $_object_data = []) |
||
25 | { |
||
26 | parent::__construct($client, $_object_data); |
||
27 | |||
28 | if (!ArrayLib::is_associative($_object_data)) { |
||
29 | throw new InvalidArgumentException("Model::__construct() expects an associative array"); |
||
30 | } |
||
31 | |||
32 | if (array_key_exists('model', $_object_data)) $this->model = $_object_data['model']; |
||
33 | $model = $this->model; |
||
34 | |||
35 | foreach ($_object_data as $field => $value) { |
||
36 | if (is_array($value)) { |
||
37 | if (ArrayLib::is_associative($value)) { |
||
38 | // Model or Object |
||
39 | if (!array_key_exists('field_name', $value)) { |
||
40 | $this->$field = ObjectFactory::make($client, $value); |
||
41 | } else { |
||
42 | // Has One Relation - Deprecated |
||
43 | $name = $value['field_name']; |
||
44 | $this->$name = $value['field_value']; |
||
45 | $this->$field = ObjectFactory::make($client, $value['object_data']); |
||
46 | } |
||
47 | |||
48 | // Setup the reverse relationship unless it's already setup |
||
49 | if (!property_exists($this->$field, $model)) { |
||
50 | $this->$field->$model = $this; |
||
51 | } |
||
52 | } else { |
||
53 | // HasMany / ManyMany Relation |
||
54 | $list = []; |
||
55 | foreach ($value as $item) { |
||
56 | if (ArrayLib::is_associative($item)) { |
||
57 | $list = ObjectFactory::make($client, $item); |
||
58 | } else { |
||
59 | $list = $item; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | $this->$field = $list; |
||
64 | } |
||
65 | } else { |
||
66 | $this->$field = $value; |
||
67 | } |
||
105 |