Completed
Pull Request — master (#9)
by Tomáš
06:29
created

Sculpin   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 23.14 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 14
dl 28
loc 121
ccs 0
cts 70
cp 0
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
D run() 28 65 16

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
/*
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\Core;
13
14
use Symplify\PHP7_Sculpin\Core\Converter\ConverterManager;
15
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents;
16
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent;
17
use Symplify\PHP7_Sculpin\Core\Formatter\FormatterManager;
18
use Symplify\PHP7_Sculpin\Core\Generator\GeneratorManager;
19
use Symplify\PHP7_Sculpin\Core\Io\ConsoleIo;
20
use Symplify\PHP7_Sculpin\Core\Output\SourceOutput;
21
use Symplify\PHP7_Sculpin\Core\Output\FilesystemWriter;
22
use Symplify\PHP7_Sculpin\Core\Permalink\SourcePermalinkFactory;
23
use Symplify\PHP7_Sculpin\Core\Source\DataSourceInterface;
24
use Symplify\PHP7_Sculpin\Core\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
    public function __construct(
65
        EventDispatcherInterface $eventDispatcher,
66
        SourcePermalinkFactory $permalinkFactory,
67
        FilesystemWriter $writer,
68
        GeneratorManager $generatorManager,
69
        FormatterManager $formatterManager,
70
        ConverterManager $converterManager,
71
        ConsoleIo $consoleIo
72
    ) {
73
        $this->eventDispatcher = $eventDispatcher;
74
        $this->permalinkFactory = $permalinkFactory;
75
        $this->writer = $writer;
76
        $this->generatorManager = $generatorManager;
77
        $this->formatterManager = $formatterManager;
78
        $this->converterManager = $converterManager;
79
        $this->consoleIo = $consoleIo;
80
    }
81
82
    public function run(DataSourceInterface $dataSource, SourceSet $sourceSet)
83
    {
84
        $found = false;
85
86
        $dataSource->refresh($sourceSet);
87
88
        $this->eventDispatcher->dispatch(SculpinEvents::EVENT_BEFORE_RUN, new SourceSetEvent($sourceSet));
89
90
        if ($updatedSources = array_filter($sourceSet->updatedSources(), function ($source) {
91
            return !$source->isGenerated();
92
        })) {
93
            if (!$found) {
94
                $this->consoleIo->write('Detected new or updated files');
95
                $found = true;
96
            }
97
98
            foreach ($updatedSources as $source) {
99
                $this->generatorManager->generate($source, $sourceSet);
100
            }
101
        }
102
103
        foreach ($sourceSet->updatedSources() as $source) {
104
            $permalink = $this->permalinkFactory->create($source);
105
            $source->setPermalink($permalink);
106
            $source->data()->set('url', $permalink->relativeUrlPath());
107
        }
108
109 View Code Duplication
        if ($updatedSources = $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...
110
            if (!$found) {
111
                $this->consoleIo->write('Detected new or updated files');
112
                $found = true;
113
            }
114
115
            foreach ($updatedSources as $source) {
116
                $this->converterManager->convertSource($source);
117
118
                if ($source->canBeFormatted()) {
119
                    $source->data()->set('blocks', $this->formatterManager->formatSourceBlocks($source));
120
                }
121
            }
122
        }
123
124 View Code Duplication
        if ($updatedSources = $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...
125
            if (!$found) {
126
                $this->consoleIo->write('Detected new or updated files');
127
                $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...
128
            }
129
130
            foreach ($updatedSources as $source) {
131
                if ($source->canBeFormatted()) {
132
                    $source->setFormattedContent($this->formatterManager->formatSourcePage($source));
133
                } else {
134
                    $source->setFormattedContent($source->content());
135
                }
136
            }
137
        }
138
139
        foreach ($sourceSet->updatedSources() as $source) {
140
            if ($source->isGenerator() || $source->shouldBeSkipped()) {
141
                continue;
142
            }
143
144
            $this->writer->write(new SourceOutput($source));
145
        }
146
    }
147
}
148