We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 17 |
| Paths | 151 |
| Total Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 56 |
| CRAP Score | 17.0015 |
| 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 |
||
| 96 | 58 | public function connectionFromArraySlice(array $arraySlice, $args, array $meta): Connection |
|
| 97 | { |
||
| 98 | 58 | $connectionArguments = $this->getOptionsWithDefaults( |
|
| 99 | 58 | $args instanceof Argument ? $args->getRawArguments() : $args, |
|
| 100 | [ |
||
| 101 | 58 | 'after' => '', |
|
| 102 | 'before' => '', |
||
| 103 | 'first' => null, |
||
| 104 | 'last' => null, |
||
| 105 | ] |
||
| 106 | ); |
||
| 107 | 58 | $arraySliceMetaInfo = $this->getOptionsWithDefaults( |
|
| 108 | 58 | $meta, |
|
| 109 | [ |
||
| 110 | 58 | 'sliceStart' => 0, |
|
| 111 | 'arrayLength' => 0, |
||
| 112 | ] |
||
| 113 | ); |
||
| 114 | |||
| 115 | 58 | $arraySliceLength = \count($arraySlice); |
|
| 116 | 58 | $after = $connectionArguments['after']; |
|
| 117 | 58 | $before = $connectionArguments['before']; |
|
| 118 | 58 | $first = $connectionArguments['first']; |
|
| 119 | 58 | $last = $connectionArguments['last']; |
|
| 120 | 58 | $sliceStart = $arraySliceMetaInfo['sliceStart']; |
|
| 121 | 58 | $arrayLength = $arraySliceMetaInfo['arrayLength']; |
|
| 122 | 58 | $sliceEnd = $sliceStart + $arraySliceLength; |
|
| 123 | 58 | $beforeOffset = $this->getOffsetWithDefault($before, $arrayLength); |
|
| 124 | 58 | $afterOffset = $this->getOffsetWithDefault($after, -1); |
|
| 125 | |||
| 126 | 58 | $startOffset = \max($sliceStart - 1, $afterOffset, -1) + 1; |
|
| 127 | 58 | $endOffset = \min($sliceEnd, $beforeOffset, $arrayLength); |
|
| 128 | |||
| 129 | 58 | if (\is_numeric($first)) { |
|
| 130 | 36 | if ($first < 0) { |
|
| 131 | 1 | throw new \InvalidArgumentException('Argument "first" must be a non-negative integer'); |
|
| 132 | } |
||
| 133 | 35 | $endOffset = \min($endOffset, $startOffset + $first); |
|
| 134 | } |
||
| 135 | |||
| 136 | 57 | if (\is_numeric($last)) { |
|
| 137 | 16 | if ($last < 0) { |
|
| 138 | 1 | throw new \InvalidArgumentException('Argument "last" must be a non-negative integer'); |
|
| 139 | } |
||
| 140 | |||
| 141 | 15 | $startOffset = \max($startOffset, $endOffset - $last); |
|
| 142 | } |
||
| 143 | |||
| 144 | // If supplied slice is too large, trim it down before mapping over it. |
||
| 145 | 56 | $offset = \max($startOffset - $sliceStart, 0); |
|
| 146 | 56 | $length = ($arraySliceLength - ($sliceEnd - $endOffset)) - $offset; |
|
| 147 | |||
| 148 | 56 | $slice = \array_slice( |
|
| 149 | 56 | $arraySlice, |
|
| 150 | 56 | $offset, |
|
| 151 | 56 | $length |
|
| 152 | ); |
||
| 153 | |||
| 154 | 56 | $edges = []; |
|
| 155 | |||
| 156 | 56 | foreach ($slice as $index => $value) { |
|
| 157 | 53 | $cursor = $this->offsetToCursor($startOffset + $index); |
|
| 158 | 53 | if ($this->edgeCallback) { |
|
| 159 | 1 | $edge = ($this->edgeCallback)($cursor, $value, $index); |
|
| 160 | 1 | if (!($edge instanceof EdgeInterface)) { |
|
| 161 | 1 | throw new \InvalidArgumentException(\sprintf('The $edgeCallback of the ConnectionBuilder must return an instance of EdgeInterface')); |
|
| 162 | } |
||
| 163 | } else { |
||
| 164 | 52 | $edge = new Edge($cursor, $value); |
|
| 165 | } |
||
| 166 | 53 | $edges[] = $edge; |
|
| 167 | } |
||
| 168 | |||
| 169 | 56 | $firstEdge = $edges[0] ?? null; |
|
| 170 | 56 | $lastEdge = \end($edges); |
|
| 171 | 56 | $lowerBound = $after ? ($afterOffset + 1) : 0; |
|
| 172 | 56 | $upperBound = $before ? $beforeOffset : $arrayLength; |
|
| 173 | |||
| 174 | 56 | $pageInfo = new PageInfo( |
|
| 175 | 56 | $firstEdge instanceof EdgeInterface ? $firstEdge->getCursor() : null, |
|
| 176 | 56 | $lastEdge instanceof EdgeInterface ? $lastEdge->getCursor() : null, |
|
| 177 | 56 | null !== $last ? $startOffset > $lowerBound : false, |
|
| 178 | 56 | null !== $first ? $endOffset < $upperBound : false |
|
| 179 | ); |
||
| 180 | |||
| 181 | 56 | if ($this->connectionCallback) { |
|
| 182 | 1 | $connection = ($this->connectionCallback)($edges, $pageInfo); |
|
| 183 | 1 | if (!($connection instanceof ConnectionInterface)) { |
|
| 184 | throw new \InvalidArgumentException(\sprintf('The $connectionCallback of the ConnectionBuilder must return an instance of ConnectionInterface')); |
||
| 185 | } |
||
| 186 | |||
| 187 | 1 | return $connection; |
|
| 188 | } |
||
| 189 | |||
| 190 | 55 | return new Connection($edges, $pageInfo); |
|
| 191 | } |
||
| 192 | |||
| 301 |