Passed
Pull Request — master (#26)
by Pierre
27:01 queued 02:03
created

src/Command/CheckGherkinCodeStyle.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Exception\ParserException;
22
use Behat\Gherkin\Parser;
23
use KawaiiGherkin\FeatureResolve;
24
use KawaiiGherkin\Formatter\Background;
25
use KawaiiGherkin\Formatter\ComposedFormatter;
26
use KawaiiGherkin\Formatter\FeatureDescription;
27
use KawaiiGherkin\Formatter\Scenario;
28
use KawaiiGherkin\Formatter\Step;
29
use KawaiiGherkin\Formatter\Tags;
30
use SebastianBergmann\Diff\Differ;
31
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Input\InputArgument;
34
use Symfony\Component\Console\Input\InputInterface;
35
use Symfony\Component\Console\Input\InputOption;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
/**
39
 * @author  Jefersson Nathan  <[email protected]>
40
 * @license MIT
41
 */
42
final class CheckGherkinCodeStyle extends Command
43
{
44
    /**
45
     * @var Parser
46
     */
47
    private $parser;
48
49
    /**
50
     * {@inheritDoc}
51
     *
52
     * @param Parser $parser
53 2
     */
54
    public function __construct($name, $parser)
55 2
    {
56 2
        parent::__construct('Kawaii Gherkin');
57 2
        $this->parser = $parser;
58
    }
59
60
    /**
61
     * {@inheritDoc}
62 2
     */
63
    protected function configure()
64
    {
65 2
        $this
66 2
            ->setName('gherkin:check')
67 2
            ->setDescription('Find wrong gherkin code styled')
68 2
            ->addArgument(
69 2
                'directory',
70 2
                InputArgument::REQUIRED,
71
                'Path to find *.feature files'
72 2
            )
73 2
            ->addOption(
74 2
                'align',
75 2
                null,
76 2
                InputOption::VALUE_OPTIONAL,
77 2
                'Side to align statement (right or left). Default right',
78
                'left'
79 2
            );
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     *
85
     * @throws \InvalidArgumentException
86
     * @throws \RuntimeException
87
     * @throws ParserException
88 2
     */
89
    protected function execute(InputInterface $input, OutputInterface $output)
90 2
    {
91 2
        $align = $input->getOption('align') === Step::ALIGN_TO_LEFT
92 2
            ? Step::ALIGN_TO_LEFT
93
            : Step::ALIGN_TO_RIGHT;
94 2
95 2
        $directory = $input->getArgument('directory');
96
        $finder    = (new FeatureResolve($directory))->__invoke();
0 ignored issues
show
It seems like $directory defined by $input->getArgument('directory') on line 95 can also be of type array<integer,string> or null; however, KawaiiGherkin\FeatureResolve::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
97 2
98
        $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
99
100 2
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
101
        foreach ($finder as $file) {
102 1
103 1
            $fileContent            = $file->getContents();
104 1
            $contentWithoutComments = $this->removeComments($fileContent);
105
            $feature                = $this->parser->parse($fileContent);
106 1
107 1
            $formatted = (new ComposedFormatter(
108 1
                new FeatureDescription(),
109 1
                new Background($align),
110 1
                new Scenario($align),
111
                new Tags()
112 1
            ))
113
                ->__invoke($feature);
114 1
115
            if ($formatted !== $contentWithoutComments) {
116 1
117 1
                if (! defined('FAILED')) {
118
                    define('FAILED', true);
119
                }
120 1
121
                $outputBuilder = new UnifiedDiffOutputBuilder("--- Original\n+++ Expected\n", false);
122 1
                $diff = new Differ($outputBuilder);
123 1
124
                $output->writeln('<error>Wrong style: ' . $file->getRealPath() . '</error>');
125
                $output->writeln($diff->diff($contentWithoutComments, $formatted));
126
            }
127 2
        }
128 1
129
        if (defined('FAILED')) {
130
            return 1;
131 1
        }
132 1
133
        $output->writeln('<bg=green;fg=white>     Everything is OK!     </>');
134
    }
135
136
    /**
137
     * @param string $fileContent
138
     *
139 1
     * @return string
140
     */
141 1
    private function removeComments($fileContent)
142 1
    {
143 1
        return rtrim(
144 1
            implode(
145 1
                array_filter(
146 1
                    array_map(
147 1
                        function ($line) {
148
                            if (0 === mb_strpos(ltrim($line), '#')) {
149
                                return '';
150 1
                            }
151 1
152 1
                            return rtrim($line) . "\n";
153
                        },
154
                        explode("\n", $fileContent)
155
                    )
156 1
                )
157
            )
158
        ) . "\n";
159
    }
160
}
161