Conditions | 7 |
Paths | 6 |
Total Lines | 60 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
44 | public function with( |
||
45 | string $relationName, |
||
46 | string $relationAlias, |
||
47 | ?string $parentAlias = null, |
||
48 | string $joinType = 'left', |
||
49 | ?string $extraJoinCondition = null, |
||
50 | array $extraJoinParams = [] |
||
51 | ): self { |
||
52 | $mainTable = $this->tableCollection->getMainTable(); |
||
53 | |||
54 | $parentAlias = $parentAlias ?? $mainTable->alias; |
||
55 | $parentClassName = $this->tableCollection->byAlias($parentAlias)->className; |
||
56 | static::checkClassIsActiveRecord($parentClassName); |
||
57 | |||
58 | /** @var ActiveRecord $inst */ |
||
59 | $inst = new $parentClassName(); |
||
60 | $methodName = 'get'.ucfirst($relationName); |
||
61 | if(!method_exists($inst, $methodName)) { |
||
62 | throw new QueryRelationManagerException("method {$parentClassName}::{$methodName}() not exists"); |
||
63 | } |
||
64 | |||
65 | /** @var ActiveQuery $activeQuery */ |
||
66 | $activeQuery = $inst->$methodName(); |
||
67 | if(!($activeQuery instanceof ActiveQuery)) { |
||
|
|||
68 | throw new QueryRelationManagerException( |
||
69 | "method {$parentClassName}::{$methodName}() returned non-ActiveQuery instance" |
||
70 | ); |
||
71 | } |
||
72 | |||
73 | if($activeQuery->via) { |
||
74 | throw new QueryRelationManagerException('cannot use relations with "via" section yet'); |
||
75 | } |
||
76 | if(!is_array($activeQuery->link) || !count($activeQuery->link)) { |
||
77 | throw new QueryRelationManagerException('cannot use relations without "link" section'); |
||
78 | } |
||
79 | |||
80 | /** @var string $className */ |
||
81 | $className = $activeQuery->modelClass; |
||
82 | |||
83 | if($activeQuery->multiple) { |
||
84 | return $this->withMultiple( |
||
85 | $relationName, |
||
86 | $className, |
||
87 | $relationAlias, |
||
88 | $parentAlias, |
||
89 | $activeQuery->link, |
||
90 | $joinType, |
||
91 | $extraJoinCondition, |
||
92 | $extraJoinParams |
||
93 | ); |
||
94 | } else { |
||
95 | return $this->withSingle( |
||
96 | $relationName, |
||
97 | $className, |
||
98 | $relationAlias, |
||
99 | $parentAlias, |
||
100 | $activeQuery->link, |
||
101 | $joinType, |
||
102 | $extraJoinCondition, |
||
103 | $extraJoinParams |
||
104 | ); |
||
186 |