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 |
||
8 | use Operador\Contracts\StageInterface; |
||
9 | |||
10 | class RepositoryDetectedPipeline implements StageInterface |
||
11 | { |
||
12 | View Code Duplication | public function __invoke($eloquentClasses) |
|
37 | public static function make($eloquentClasses) |
||
38 | { |
||
39 | |||
40 | |||
41 | |||
42 | // Returns 21 |
||
43 | $entitys = $pipeline->process($eloquentClasses); |
||
44 | |||
45 | |||
46 | |||
47 | // Re-usable Pipelines |
||
48 | // Because the PipelineInterface is an extension of the StageInterface pipelines can be re-used as stages. This creates a highly composable model to create complex execution patterns while keeping the cognitive load low. |
||
49 | |||
50 | // For example, if we'd want to compose a pipeline to process API calls, we'd create something along these lines: |
||
51 | |||
52 | $processApiRequest = (new Pipeline) |
||
53 | ->pipe(new ExecuteHttpRequest) // 2 |
||
54 | ->pipe(new ParseJsonResponse); // 3 |
||
55 | |||
56 | $pipeline = (new Pipeline) |
||
57 | ->pipe(new ConvertToPsr7Request) // 1 |
||
58 | ->pipe($processApiRequest) // (2,3) |
||
59 | ->pipe(new ConvertToResponseDto); // 4 |
||
60 | |||
61 | $pipeline->process(new DeleteBlogPost($postId)); |
||
62 | } |
||
63 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.