ConcernedAboutSeparation   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 8
dl 0
loc 137
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getName() 0 4 1
A getDescription() 0 4 1
A getSolution() 0 4 1
A tearDown() 0 4 1
A check() 0 18 4
A getType() 0 4 1
A getArgs() 0 39 2
1
<?php
2
3
namespace PhpSchool\LearnYouPhp\Exercise;
4
5
use Faker\Generator;
6
use PhpParser\Parser;
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\SelfCheck;
13
use PhpSchool\PhpWorkshop\ExerciseCheck\StdOutExerciseCheck;
14
use PhpSchool\PhpWorkshop\ExerciseDispatcher;
15
use PhpSchool\PhpWorkshop\Input\Input;
16
use PhpSchool\PhpWorkshop\Result\Failure;
17
use PhpSchool\PhpWorkshop\Result\ResultInterface;
18
use PhpSchool\PhpWorkshop\Result\Success;
19
use PhpSchool\PhpWorkshop\Solution\DirectorySolution;
20
use PhpSchool\PhpWorkshop\Solution\SolutionInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use PhpParser\Node\Expr\Include_;
23
24
/**
25
 * Class ConcernedAboutSeparation
26
 * @package PhpSchool\LearnYouPhp\Exercise
27
 * @author Aydin Hassan <[email protected]>
28
 */
29
class ConcernedAboutSeparation extends AbstractExercise implements ExerciseInterface, CliExercise, SelfCheck
30
{
31
    use TemporaryDirectoryTrait;
32
33
    /**
34
     * @var Filesystem
35
     */
36
    private $filesystem;
37
38
    /**
39
     * @var Generator
40
     */
41
    private $faker;
42
43
    /**
44
     * @var Parser
45
     */
46
    private $parser;
47
48
    /**
49
     * @param Filesystem $filesystem
50
     * @param Generator $faker
51
     * @param Parser $parser
52
     */
53
    public function __construct(Filesystem $filesystem, Generator $faker, Parser $parser)
54
    {
55
        $this->filesystem = $filesystem;
56
        $this->faker = $faker;
57
        $this->parser = $parser;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getName()
64
    {
65
        return 'Concerned about Separation?';
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function getDescription()
72
    {
73
        return 'Separate code and utilise files and classes';
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getArgs()
80
    {
81
        $folder = $this->getTemporaryPath();
82
83
        $files = [
84
            "learnyouphp.dat",
85
            "learnyouphp.txt",
86
            "learnyouphp.sql",
87
            "txt",
88
            "sql",
89
            "api.html",
90
            "html",
91
            "README.md",
92
            "CHANGELOG.md",
93
            "LICENCE.md",
94
            "md",
95
            "data.json",
96
            "json",
97
            "data.dat",
98
            "words.dat",
99
            "w00t.dat",
100
            "w00t.txt",
101
            "wrrrrongdat",
102
            "dat",
103
        ];
104
105
        $this->filesystem->mkdir($folder);
106
        array_walk($files, function ($file) use ($folder) {
107
            $this->filesystem->dumpFile(sprintf('%s/%s', $folder, $file), '');
108
        });
109
110
        $ext = '';
111
        while ($ext === '') {
112
            $index = array_rand($files);
113
            $ext = pathinfo($files[$index], PATHINFO_EXTENSION);
114
        }
115
116
        return [$folder, $ext];
117
    }
118
119
    /**
120
     * @return SolutionInterface
121
     */
122
    public function getSolution()
123
    {
124
        return DirectorySolution::fromDirectory(__DIR__ . '/../../exercises/concerned-about-separation/solution');
125
    }
126
127
    /**
128
     * @return null
129
     */
130
    public function tearDown()
131
    {
132
        $this->filesystem->remove($this->getTemporaryPath());
133
    }
134
135
    /**
136
     * @param Input $input
137
     * @return ResultInterface
138
     */
139
    public function check(Input $input)
140
    {
141
        $statements = $this->parser->parse(file_get_contents($input->getArgument('program')));
142
143
        $include = null;
144
        foreach ($statements as $statement) {
0 ignored issues
show
Bug introduced by
The expression $statements of type array<integer,object<PhpParser\Node>>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
145
            if ($statement instanceof Include_) {
146
                $include = $statement;
147
                break;
148
            }
149
        }
150
151
        if (null === $include) {
152
            return Failure::fromNameAndReason($this->getName(), 'No require statement found');
153
        }
154
155
        return new Success($this->getName());
156
    }
157
158
    /**
159
     * @return ExerciseType
160
     */
161
    public function getType()
162
    {
163
        return ExerciseType::CLI();
164
    }
165
}
166