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

src/Command/FixGherkinCodeStyle.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\Parser;
22
use KawaiiGherkin\FeatureResolve;
23
use KawaiiGherkin\Formatter\Background;
24
use KawaiiGherkin\Formatter\ComposedFormatter;
25
use KawaiiGherkin\Formatter\FeatureDescription;
26
use KawaiiGherkin\Formatter\Scenario;
27
use KawaiiGherkin\Formatter\Step;
28
use KawaiiGherkin\Formatter\Tags;
29
use Symfony\Component\Console\Command\Command;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Input\InputOption;
33
use Symfony\Component\Console\Output\OutputInterface;
34
use Symfony\Component\Finder\Finder;
35
36
/**
37
 * @author Jefersson Nathan  <[email protected]>
38
 * @license MIT
39
 */
40
final class FixGherkinCodeStyle extends Command
41
{
42
    /**
43
     * @var Parser
44
     */
45
    private $parser;
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @param Parser $parser
51
     */
52
    public function __construct($name, Parser $parser)
53
    {
54
        parent::__construct('Kawaii Gherkin');
55
        $this->parser = $parser;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61
    protected function configure()
62
    {
63
        $this
64
            ->setName('gherkin:fix')
65
            ->setDescription('Fix gherkin code style')
66
            ->addArgument(
67
                'directory',
68
                InputArgument::REQUIRED,
69
                'Path to find *.feature files'
70
            )
71
            ->addOption(
72
                'align',
73
                null,
74
                InputOption::VALUE_OPTIONAL,
75
                'Side to align statement (right or left). Default right',
76
                'left'
77
            );
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        $align = $input->getOption('align') === Step::ALIGN_TO_LEFT
86
            ? Step::ALIGN_TO_LEFT
87
            : Step::ALIGN_TO_RIGHT;
88
89
        $directory = $input->getArgument('directory');
90
        $finder    = (new FeatureResolve($directory))->__invoke();
0 ignored issues
show
It seems like $directory defined by $input->getArgument('directory') on line 89 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...
91
92
        $output->writeln("\nFinding files on <info>" . $directory . "</info>\n");
93
94
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
95
        foreach ($finder as $file) {
96
97
            $fileContent = $file->getContents();
98
            $feature     = $this->parser->parse($fileContent);
99
100
            $formatted = (new ComposedFormatter(
101
                new FeatureDescription(),
102
                new Background($align),
103
                new Scenario($align),
104
                new Tags()
105
            ))
106
                ->__invoke($feature);
107
108
            $filePointer = $file->openFile('w');
109
            $filePointer->fwrite($formatted);
110
111
            $output->writeln('<info>' . $file->getRealPath() . '</info>');
112
        }
113
    }
114
}
115