Completed
Pull Request — master (#1)
by Jefersson
03:34 queued 39s
created

CheckGherkinCodeStyle::removeComments()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 11
nc 1
nop 1
crap 6
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace KawaiiGherkin\Command;
20
21
use Behat\Gherkin\Parser;
22
use KawaiiGherkin\Formatter\Background;
23
use KawaiiGherkin\Formatter\FeatureDescription;
24
use KawaiiGherkin\Formatter\Scenario;
25
use KawaiiGherkin\Formatter\Step;
26
use KawaiiGherkin\Formatter\Tags;
27
use SebastianBergmann\Diff\Differ;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputArgument;
30
use Symfony\Component\Console\Input\InputInterface;
31
use Symfony\Component\Console\Output\OutputInterface;
32
use Symfony\Component\Finder\Finder;
33
34
/**
35
 * @author  Jefersson Nathan  <[email protected]>
36
 * @license MIT
37
 */
38
final class CheckGherkinCodeStyle extends Command
39
{
40
    /**
41
     * @var Parser
42
     */
43
    private $parser;
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * @param Parser $parser
49
     */
50 1
    public function __construct($name, $parser)
51
    {
52 1
        parent::__construct('Kawaii Gherkin');
53 1
        $this->parser = $parser;
54 1
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59 1
    protected function configure()
60
    {
61
        $this
62 1
            ->setName('kawaii:gherkin:check')
63 1
            ->setDescription('Fix gherkin code style')
64 1
            ->addArgument(
65 1
                'directory',
66 1
                InputArgument::REQUIRED,
67 1
                'Path to find *.feature files'
68
            )
69 1
            ->addArgument(
70 1
                'align',
71 1
                InputArgument::OPTIONAL,
72 1
                'Side to align statement (right or left). Default right'
73
            );
74 1
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 1
    protected function execute(InputInterface $input, OutputInterface $output)
1 ignored issue
show
Complexity introduced by
This operation has 3760 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
80
    {
81 1
        $align = $input->getArgument('align') === Step::ALIGN_TO_LEFT
82
            ? Step::ALIGN_TO_LEFT
83 1
            : Step::ALIGN_TO_RIGHT;
84
85 1
        $directory = $input->getArgument('directory');
86 1
        $finder    = new Finder();
87
        $finder
88 1
            ->files()
89 1
            ->in($directory)
90 1
            ->name('*.feature');
91
92 1
        $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
93
94
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
95 1
        foreach ($finder as $file) {
96
97
            $fileContent            = $file->getContents();
98
            $contentWithoutComments = $this->removeComments($fileContent);
1 ignored issue
show
Comprehensibility Naming introduced by
The variable name $contentWithoutComments exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
99
100
            $feature            = $this->parser->parse($fileContent);
101
            $tagFormatter       = new Tags();
102
            $featureDescription = new FeatureDescription();
103
            $background         = new Background($align);
104
            $scenario           = new Scenario($align);
105
106
            $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . PHP_EOL : '';
107
            $formatted .= $featureDescription->format($feature->getTitle(), explode(PHP_EOL, $feature->getDescription())) . PHP_EOL . PHP_EOL;
108
            $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . PHP_EOL : '';
109
            $formatted .= $feature->hasScenarios() ? $scenario->format($feature->getScenarios()) : '';
110
111
            if ($formatted !== $contentWithoutComments) {
112
113
                if (! defined('FAILED')) {
114
                    define('FAILED', true);
115
                }
116
117
                $diff = new Differ("--- Original\n+++ Expected\n", false);
118
119
                $output->writeln('<error>Wrong style: ' . $file->getRealPath() . '</error>');
120
                $output->writeln($diff->diff($contentWithoutComments, $formatted));
121
            }
122
        }
123
124 1
        if (defined('FAILED')) {
125
            return 1;
126
        }
127
128 1
        $output->writeln('<bg=green;fg=white>     Everything is OK!     </>');
129 1
    }
130
131
    /**
132
     * @param string $fileContent
133
     *
134
     * @return string
135
     */
136
    private function removeComments($fileContent)
137
    {
138
        return rtrim(
139
            implode(
140
                array_filter(
141
                    array_map(
142
                        function ($line) {
143
                            if (0 === mb_strpos(ltrim($line), '#')) {
144
                                return '';
145
                            }
146
147
                            return rtrim($line) . PHP_EOL;
148
                        },
149
                        explode("\n", $fileContent)
150
                    )
151
                )
152
            )
153
        ) . PHP_EOL;
154
    }
155
}
156