Conditions | 9 |
Paths | 7 |
Total Lines | 56 |
Code Lines | 27 |
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 |
||
50 | public function makeDefinition() |
||
51 | { |
||
52 | //Find only first level children stored in the same collection |
||
53 | $children = $this->findChildren(true, true); |
||
54 | |||
55 | if (empty($children)) { |
||
56 | //Nothing to inherit |
||
57 | return $this->schema->getClass(); |
||
58 | } |
||
59 | |||
60 | //We must sort child in order or unique fields |
||
61 | uasort($children, [$this, 'sortChildren']); |
||
62 | |||
63 | //Fields which are common for parent and child models |
||
64 | $commonFields = $this->schema->getReflection()->getFields(); |
||
65 | |||
66 | $definition = []; |
||
67 | foreach ($children as $schema) { |
||
68 | //Child document fields |
||
69 | $fields = $schema->getReflection()->getFields(); |
||
70 | |||
71 | if (empty($fields)) { |
||
72 | throw new DefinitionException( |
||
73 | "Child document '{$schema->getClass()}' of {$this->schema->getClass()} does not have any fields" |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | $uniqueField = null; |
||
78 | if (empty($commonFields)) { |
||
79 | //Parent did not declare any fields, happen sometimes |
||
80 | $commonFields = $fields; |
||
81 | $uniqueField = key($fields); |
||
82 | } else { |
||
83 | foreach ($fields as $field => $type) { |
||
84 | if (!isset($commonFields[$field])) { |
||
85 | if (empty($uniqueField)) { |
||
86 | $uniqueField = $field; |
||
87 | } |
||
88 | |||
89 | //New non unique field (must be excluded from analysis) |
||
90 | $commonFields[$field] = true; |
||
91 | } |
||
92 | } |
||
93 | } |
||
94 | |||
95 | if (empty($uniqueField)) { |
||
96 | throw new DefinitionException( |
||
97 | "Child document {$schema} of {$this} does not have any unique field" |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | $definition[$uniqueField] = $schema->getClass(); |
||
102 | } |
||
103 | |||
104 | return $definition; |
||
105 | } |
||
106 | |||
196 | } |