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