We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
18 | class Paginator |
||
19 | { |
||
20 | const MODE_REGULAR = false; |
||
21 | const MODE_PROMISE = true; |
||
22 | |||
23 | /** |
||
24 | * @var callable |
||
25 | */ |
||
26 | private $fetcher; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $promise; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $totalCount; |
||
37 | |||
38 | /** |
||
39 | * @param callable $fetcher |
||
40 | * @param bool $promise |
||
41 | */ |
||
42 | 14 | public function __construct(callable $fetcher, $promise = self::MODE_REGULAR) |
|
47 | |||
48 | /** |
||
49 | * @param Argument|array $args |
||
50 | * @param int|callable $total |
||
51 | * @param array $callableArgs |
||
52 | * |
||
53 | * @return Connection |
||
54 | */ |
||
55 | 7 | public function backward($args, $total, array $callableArgs = []) |
|
72 | |||
73 | /** |
||
74 | * @param Argument|array $args |
||
75 | * |
||
76 | * @return Connection |
||
77 | */ |
||
78 | 7 | public function forward($args) |
|
79 | { |
||
80 | 7 | $args = $this->protectArgs($args); |
|
81 | 7 | $limit = $args['first']; |
|
82 | 7 | $offset = ConnectionBuilder::getOffsetWithDefault($args['after'], 0); |
|
83 | |||
84 | // If we don't have a cursor or if it's not valid, then we must not use the slice method |
||
85 | 7 | if (!is_numeric(ConnectionBuilder::cursorToOffset($args['after'])) || !$args['after']) { |
|
86 | 4 | $entities = call_user_func($this->fetcher, $offset, $limit + 1); |
|
87 | |||
88 | 4 | return $this->handleEntities($entities, function ($entities) use ($args) { |
|
89 | 3 | return ConnectionBuilder::connectionFromArray($entities, $args); |
|
90 | 4 | }); |
|
91 | } else { |
||
92 | 3 | $entities = call_user_func($this->fetcher, $offset, $limit + 2); |
|
93 | |||
94 | 3 | View Code Duplication | return $this->handleEntities($entities, function ($entities) use ($args, $offset) { |
95 | 3 | return ConnectionBuilder::connectionFromArraySlice($entities, $args, [ |
|
96 | 3 | 'sliceStart' => $offset, |
|
97 | 3 | 'arrayLength' => $offset + count($entities), |
|
98 | ]); |
||
99 | 3 | }); |
|
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param Argument|array $args |
||
105 | * @param int|callable $total |
||
106 | * @param array $callableArgs |
||
107 | * |
||
108 | * @return Connection |
||
109 | */ |
||
110 | 5 | public function auto($args, $total, $callableArgs = []) |
|
111 | { |
||
112 | 5 | $args = $this->protectArgs($args); |
|
113 | |||
114 | 5 | if ($args['last']) { |
|
115 | 3 | $connection = $this->backward($args, $total, $callableArgs); |
|
116 | } else { |
||
117 | 2 | $connection = $this->forward($args); |
|
118 | } |
||
119 | |||
120 | 5 | if ($this->promise) { |
|
121 | 1 | $connection->then(function (Connection $connection) use ($total, $callableArgs) { |
|
122 | $connection->totalCount = $this->computeTotalCount($total, $callableArgs); |
||
123 | 1 | }); |
|
124 | } else { |
||
125 | 4 | $connection->totalCount = $this->computeTotalCount($total, $callableArgs); |
|
126 | } |
||
127 | |||
128 | 5 | return $connection; |
|
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param array|object $entities An array of entities to paginate or a promise |
||
133 | * @param callable $callback |
||
134 | * |
||
135 | * @return Connection|object A connection or a promise |
||
136 | */ |
||
137 | 14 | private function handleEntities($entities, callable $callback) |
|
145 | |||
146 | /** |
||
147 | * @param Argument|array $args |
||
148 | * |
||
149 | * @return Argument |
||
150 | */ |
||
151 | 14 | private function protectArgs($args) |
|
155 | |||
156 | /** |
||
157 | * @param int $total |
||
158 | * @param array $callableArgs |
||
159 | * |
||
160 | * @return int|mixed |
||
161 | */ |
||
162 | 8 | private function computeTotalCount($total, array $callableArgs = []) |
|
172 | } |
||
173 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.