Conditions | 17 |
Paths | 3 |
Total Lines | 58 |
Code Lines | 42 |
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 |
||
99 | public function buildComparisonFunction(array $ancestors): callable |
||
100 | { |
||
101 | if (0 == count($ancestors)) { |
||
102 | $msg = Messages::orderByLeafNodeArgumentShouldBeNonEmptyArray(); |
||
103 | throw new InvalidArgumentException($msg); |
||
104 | } |
||
105 | |||
106 | $ascend = $this->isAscending ? 1 : -1; |
||
107 | |||
108 | $retVal = function ($object1, $object2) use ($ancestors, $ascend) { |
||
109 | $accessor1 = $object1; |
||
110 | $accessor2 = $object2; |
||
111 | $flag1 = null === $accessor1; |
||
112 | $flag2 = null === $accessor2; |
||
113 | foreach ($ancestors as $i => $ancestor) { |
||
114 | if ($i == 0) { |
||
115 | continue; |
||
116 | } |
||
117 | $accessor1 = $accessor1->{$ancestor}; |
||
118 | $accessor2 = $accessor2->{$ancestor}; |
||
119 | $flag1 |= null === $accessor1; |
||
120 | $flag2 |= null === $accessor2; |
||
121 | } |
||
122 | $propertyName = $this->propertyName; |
||
123 | $getter = 'get' . ucfirst($propertyName); |
||
124 | if (null !== $accessor1) { |
||
125 | $accessor1 = method_exists($accessor1, $getter) ? $accessor1->{$getter}() : $accessor1->{$propertyName}; |
||
126 | } |
||
127 | if (null !== $accessor2) { |
||
128 | $accessor2 = method_exists($accessor2, $getter) ? $accessor2->{$getter}() : $accessor2->{$propertyName}; |
||
129 | } |
||
130 | |||
131 | $flag1 |= null === $accessor1; |
||
132 | $flag2 |= null === $accessor2; |
||
133 | |||
134 | if ($flag1 && $flag2) { |
||
135 | return 0; |
||
136 | } elseif ($flag1) { |
||
137 | return $ascend * -1; |
||
138 | } elseif ($flag2) { |
||
139 | return $ascend * 1; |
||
140 | } |
||
141 | $type = $this->resourceProperty->getInstanceType(); |
||
142 | if ($type instanceof DateTime) { |
||
143 | $result = strtotime($accessor1) - strtotime($accessor2); |
||
144 | } elseif ($type instanceof StringType) { |
||
145 | $result = strcmp($accessor1, $accessor2); |
||
146 | } elseif ($type instanceof Guid) { |
||
147 | $result = strcmp($accessor1, $accessor2); |
||
148 | } else { |
||
149 | $delta = $accessor1 - $accessor2; |
||
150 | $result = (0 == $delta) ? 0 : $delta / abs($delta); |
||
151 | } |
||
152 | |||
153 | return $ascend * $result; |
||
154 | }; |
||
155 | |||
156 | return $retVal; |
||
157 | } |
||
159 |