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 TransformerResolver implements TransformerResolverContract |
||
19 | { |
||
20 | /** |
||
21 | * Transformable to transformer mappings. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $bindings = []; |
||
26 | |||
27 | /** |
||
28 | * A container used to resolve transformers. |
||
29 | * |
||
30 | * @var \Illuminate\Contracts\Container\Container |
||
31 | */ |
||
32 | protected $container; |
||
33 | |||
34 | /** |
||
35 | * A fallback transformer to return when no transformer can be resolved. |
||
36 | * |
||
37 | * @var \Flugg\Responder\Transformers\Transformer|string|callable |
||
38 | */ |
||
39 | protected $fallback; |
||
40 | |||
41 | /** |
||
42 | * Construct the resolver class. |
||
43 | * |
||
44 | * @param \Illuminate\Contracts\Container\Container $container |
||
45 | * @param \Flugg\Responder\Transformers\Transformer|string|callable $fallback |
||
46 | */ |
||
47 | 57 | public function __construct(Container $container, $fallback) |
|
52 | |||
53 | /** |
||
54 | * Register a transformable to transformer binding. |
||
55 | * |
||
56 | * @param string|array $transformable |
||
57 | * @param string|callback|null $transformer |
||
58 | * @return void |
||
59 | */ |
||
60 | 2 | public function bind($transformable, $transformer = null) |
|
66 | |||
67 | /** |
||
68 | * Resolve a transformer. |
||
69 | * |
||
70 | * @param \Flugg\Responder\Transformers\Transformer|string|callable $transformer |
||
71 | * @return \Flugg\Responder\Transformers\Transformer|callable |
||
72 | * @throws \Flugg\Responder\Exceptions\InvalidTransformerException |
||
73 | */ |
||
74 | 46 | public function resolve($transformer) |
|
86 | |||
87 | /** |
||
88 | * Resolve a transformer from the given data. |
||
89 | * |
||
90 | * @param mixed $data |
||
91 | * @return \Flugg\Responder\Transformers\Transformer|callable |
||
92 | */ |
||
93 | 30 | public function resolveFromData($data) |
|
99 | |||
100 | /** |
||
101 | * Resolve a transformer from the transformable element. |
||
102 | * |
||
103 | * @param mixed $transformable |
||
104 | * @return \Flugg\Responder\Contracts\Transformable|callable |
||
105 | */ |
||
106 | 30 | View Code Duplication | protected function resolveTransformer($transformable) |
118 | |||
119 | /** |
||
120 | * Resolve a transformable item from the given data. |
||
121 | * |
||
122 | * @param mixed $data |
||
123 | * @return mixed |
||
124 | */ |
||
125 | 30 | protected function resolveTransformableItem($data) |
|
135 | } |
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.