Completed
Pull Request — master (#11)
by Tomáš
04:15
created

Sculpin::run()   D

Complexity

Conditions 16
Paths 312

Size

Total Lines 65
Code Lines 36

Duplication

Lines 28
Ratio 43.08 %

Code Coverage

Tests 0
CRAP Score 272

Importance

Changes 0
Metric Value
dl 28
loc 65
ccs 0
cts 53
cp 0
rs 4.4063
c 0
b 0
f 0
cc 16
eloc 36
nc 312
nop 1
crap 272

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin;
13
14
use Symplify\PHP7_Sculpin\Converter\ConverterManager;
15
use Symplify\PHP7_Sculpin\Event\SculpinEvents;
16
use Symplify\PHP7_Sculpin\Event\SourceSetEvent;
17
use Symplify\PHP7_Sculpin\Formatter\FormatterManager;
18
use Symplify\PHP7_Sculpin\Generator\GeneratorManager;
19
use Symplify\PHP7_Sculpin\Io\ConsoleIo;
20
use Symplify\PHP7_Sculpin\Output\SourceOutput;
21
use Symplify\PHP7_Sculpin\Output\FilesystemWriter;
22
use Symplify\PHP7_Sculpin\Permalink\SourcePermalinkFactory;
23
use Symplify\PHP7_Sculpin\Source\DataSourceInterface;
24
use Symplify\PHP7_Sculpin\Source\SourceSet;
25
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
26
27
final class Sculpin
28
{
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $eventDispatcher;
33
34
    /**
35
     * @var SourcePermalinkFactory
36
     */
37
    private $permalinkFactory;
38
39
    /**
40
     * @var FilesystemWriter
41
     */
42
    private $writer;
43
44
    /**
45
     * @var GeneratorManager
46
     */
47
    private $generatorManager;
48
49
    /**
50
     * @var FormatterManager
51
     */
52
    private $formatterManager;
53
54
    /**
55
     * @var ConverterManager
56
     */
57
    private $converterManager;
58
59
    /**
60
     * @var ConsoleIo
61
     */
62
    private $consoleIo;
63
64
    /**
65
     * @var SourceSet
66
     */
67
    private $sourceSet;
68
69
    public function __construct(
70
        EventDispatcherInterface $eventDispatcher,
71
        SourcePermalinkFactory $permalinkFactory,
72
        FilesystemWriter $writer,
73
        GeneratorManager $generatorManager,
74
        FormatterManager $formatterManager,
75
        ConverterManager $converterManager,
76
        ConsoleIo $consoleIo,
77
        SourceSet $sourceSet
78
    ) {
79
        $this->eventDispatcher = $eventDispatcher;
80
        $this->permalinkFactory = $permalinkFactory;
81
        $this->writer = $writer;
82
        $this->generatorManager = $generatorManager;
83
        $this->formatterManager = $formatterManager;
84
        $this->converterManager = $converterManager;
85
        $this->consoleIo = $consoleIo;
86
        $this->sourceSet = $sourceSet;
87
    }
88
89
    public function run(DataSourceInterface $dataSource)
90
    {
91
        $found = false;
92
93
        $dataSource->refresh($this->sourceSet);
94
95
        $this->eventDispatcher->dispatch(SculpinEvents::BEFORE_RUN, new SourceSetEvent($this->sourceSet));
96
97
        if ($updatedSources = array_filter($this->sourceSet->updatedSources(), function ($source) {
98
            return !$source->isGenerated();
99
        })) {
100
            if (!$found) {
101
                $this->consoleIo->write('Detected new or updated files');
102
                $found = true;
103
            }
104
105
            foreach ($updatedSources as $source) {
106
                $this->generatorManager->generate($source, $this->sourceSet);
107
            }
108
        }
109
110
        foreach ($this->sourceSet->updatedSources() as $source) {
111
            $permalink = $this->permalinkFactory->create($source);
112
            $source->setPermalink($permalink);
113
            $source->data()->set('url', $permalink->relativeUrlPath());
114
        }
115
116 View Code Duplication
        if ($updatedSources = $this->sourceSet->updatedSources()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
117
            if (!$found) {
118
                $this->consoleIo->write('Detected new or updated files');
119
                $found = true;
120
            }
121
122
            foreach ($updatedSources as $source) {
123
                $this->converterManager->convertSource($source);
124
125
                if ($source->canBeFormatted()) {
126
                    $source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
127
                }
128
            }
129
        }
130
131 View Code Duplication
        if ($updatedSources = $this->sourceSet->updatedSources()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
132
            if (!$found) {
133
                $this->consoleIo->write('Detected new or updated files');
134
                $found = true;
0 ignored issues
show
Unused Code introduced by
$found is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
            }
136
137
            foreach ($updatedSources as $source) {
138
                if ($source->canBeFormatted()) {
139
                    $source->setFormattedContent($this->formatterManager->formatSourcePage($source));
140
                } else {
141
                    $source->setFormattedContent($source->content());
142
                }
143
            }
144
        }
145
146
        foreach ($this->sourceSet->updatedSources() as $source) {
147
            if ($source->isGenerator() || $source->shouldBeSkipped()) {
148
                continue;
149
            }
150
151
            $this->writer->write(new SourceOutput($source));
152
        }
153
    }
154
155
    public function resetSources()
156
    {
157
        $this->sourceSet->reset();
158
    }
159
}
160