Completed
Push — master ( 48a5d3...028360 )
by Aydin
03:14
created

ExceptionalCoding   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
c 3
b 0
f 0
lcom 1
cbo 5
dl 105
loc 105
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A getName() 4 4 1
A getDescription() 4 4 1
A getArgs() 18 18 3
A tearDown() 4 4 1
A getRequiredFunctions() 4 4 1
A getBannedFunctions() 4 4 1
A getType() 4 4 1
A configure() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use Faker\Generator;
6
use PhpSchool\PhpWorkshop\Check\FunctionRequirementsCheck;
7
use PhpSchool\PhpWorkshop\Exercise\AbstractExercise;
8
use PhpSchool\PhpWorkshop\Exercise\CliExercise;
9
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
10
use PhpSchool\PhpWorkshop\Exercise\ExerciseType;
11
use PhpSchool\PhpWorkshop\Exercise\TemporaryDirectoryTrait;
12
use PhpSchool\PhpWorkshop\ExerciseCheck\FunctionRequirementsExerciseCheck;
13
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck;
14
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
15
use Symfony\Component\Filesystem\Filesystem;
16
17 View Code Duplication
class ExceptionalCoding extends AbstractExercise implements
18
    ExerciseInterface,
19
    CliExercise,
20
    FunctionRequirementsExerciseCheck
21
{
22
    use TemporaryDirectoryTrait;
23
    
24
    /**
25
     * @var Filesystem
26
     */
27
    private $filesystem;
28
29
    /**
30
     * @var Generator
31
     */
32
    private $faker;
33
34
    /**
35
     * @param Filesystem $filesystem
36
     * @param Generator $faker
37
     */
38
    public function __construct(Filesystem $filesystem, Generator $faker)
39
    {
40
        $this->filesystem   = $filesystem;
41
        $this->faker        = $faker;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getName()
48
    {
49
        return "Exceptional Coding";
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getDescription()
56
    {
57
        return "Introduction to Exceptions";
58
    }
59
60
    /**
61
     * @return array
62
     */
63
    public function getArgs()
64
    {
65
        $this->filesystem->mkdir($this->getTemporaryPath());
66
67
        $fileCount = rand(2, 10);
68
        $realFiles = rand(1, $fileCount - 1);
69
70
        $files = [];
71
        foreach (range(1, $fileCount) as $index) {
72
            $file = sprintf('%s/%s.txt', $this->getTemporaryPath(), $this->faker->uuid);
73
            if ($index <= $realFiles) {
74
                $this->filesystem->touch($file);
75
            }
76
            $files[] = $file;
77
        }
78
79
        return $files;
80
    }
81
82
    /**
83
     * @return void
84
     */
85
    public function tearDown()
86
    {
87
        $this->filesystem->remove($this->getTemporaryPath());
88
    }
89
90
    /**
91
     * @return string[]
92
     */
93
    public function getRequiredFunctions()
94
    {
95
        return [];
96
    }
97
98
    /**
99
     * @return string[]
100
     */
101
    public function getBannedFunctions()
102
    {
103
        return ['array_filter', 'file_exists'];
104
    }
105
106
    /**
107
     * @return ExerciseType
108
     */
109
    public function getType()
110
    {
111
        return ExerciseType::CLI();
112
    }
113
114
    /**
115
     * @param ExerciseDispatcher $dispatcher
116
     */
117
    public function configure(ExerciseDispatcher $dispatcher)
118
    {
119
        $dispatcher->requireCheck(FunctionRequirementsCheck::class, $dispatcher::CHECK_AFTER);
120
    }
121
}
122