| Conditions | 5 |
| Paths | 6 |
| Total Lines | 59 |
| Code Lines | 32 |
| 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 declare(strict_types=1); |
||
| 85 | public function __resolve() |
||
| 86 | { |
||
| 87 | if (is_null($this->orm)) { |
||
| 88 | return $this->resolved; |
||
| 89 | } |
||
| 90 | |||
| 91 | if (!$this->orm instanceof Select\SourceProviderInterface) { |
||
|
|
|||
| 92 | throw new ORMException("PivotedPromise require ORM to implement SourceFactoryInterface"); |
||
| 93 | } |
||
| 94 | |||
| 95 | $table = $this->orm->getSource($this->target)->getTable(); |
||
| 96 | |||
| 97 | // getting scoped query |
||
| 98 | $root = new Select\RootLoader($this->orm, $this->target); |
||
| 99 | $query = $root->buildQuery(); |
||
| 100 | |||
| 101 | // responsible for all the scoping |
||
| 102 | $loader = new ManyToManyLoader($this->orm, $table, $this->target, $this->relationSchema); |
||
| 103 | |||
| 104 | /** @var ManyToManyLoader $loader */ |
||
| 105 | $loader = $loader->withContext($loader, [ |
||
| 106 | 'constrain' => $this->orm->getSource($this->target)->getConstrain(), |
||
| 107 | 'as' => $table, |
||
| 108 | 'method' => JoinableLoader::POSTLOAD |
||
| 109 | ]); |
||
| 110 | |||
| 111 | $query = $loader->configureQuery($query, [$this->innerKey]); |
||
| 112 | |||
| 113 | // we are going to add pivot node into virtual root node to aggregate the results |
||
| 114 | $root = new RootNode([$this->relationSchema[Relation::INNER_KEY]], $this->relationSchema[Relation::INNER_KEY]); |
||
| 115 | |||
| 116 | $node = $loader->createNode(); |
||
| 117 | $root->linkNode('output', $node); |
||
| 118 | |||
| 119 | // emulate presence of parent entity |
||
| 120 | $root->parseRow(0, [$this->innerKey]); |
||
| 121 | |||
| 122 | $iterator = $query->getIterator(); |
||
| 123 | foreach ($iterator as $row) { |
||
| 124 | $node->parseRow(0, $row); |
||
| 125 | } |
||
| 126 | $iterator->close(); |
||
| 127 | |||
| 128 | $elements = []; |
||
| 129 | $pivotData = new \SplObjectStorage(); |
||
| 130 | foreach (new Iterator($this->orm, $this->target, $root->getResult()[0]['output']) as $pivot => $entity) { |
||
| 131 | $pivotData[$entity] = $this->orm->make( |
||
| 132 | $this->relationSchema[Relation::THOUGH_ENTITY], |
||
| 133 | $pivot, |
||
| 134 | Node::MANAGED |
||
| 135 | ); |
||
| 136 | |||
| 137 | $elements[] = $entity; |
||
| 138 | } |
||
| 139 | |||
| 140 | $this->resolved = new PivotedStorage($elements, $pivotData); |
||
| 141 | $this->orm = null; |
||
| 142 | |||
| 143 | return $this->resolved; |
||
| 144 | } |
||
| 145 | } |