Completed
Push — master ( 1806e5...ab9baf )
by Jefersson
04:58
created

src/Command/FixGherkinCodeStyle.php (1 issue)

php_md.coupling_between_objects

Complexity Informational

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\Formatter\Background;
23
use KawaiiGherkin\Formatter\FeatureDescription;
24
use KawaiiGherkin\Formatter\Scenario;
25
use KawaiiGherkin\Formatter\Step;
26
use KawaiiGherkin\Formatter\Tags;
27
use Symfony\Component\Console\Command\Command;
28
use Symfony\Component\Console\Input\InputArgument;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Input\InputOption;
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 FixGherkinCodeStyle extends Command
0 ignored issues
show
The class FixGherkinCodeStyle has a coupling between objects value of 13. Consider to reduce the number of dependencies under 13.
Loading history...
39
{
40
    /**
41
     * @var Parser
42
     */
43
    private $parser;
44
45
    /**
46
     * {@inheritDoc}
47
     *
48
     * @param Parser $parser
49
     */
50
    public function __construct($name, Parser $parser)
51
    {
52
        parent::__construct('Kawaii Gherkin');
53
        $this->parser = $parser;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    protected function configure()
60
    {
61
        $this
62
            ->setName('gherkin:fix')
63
            ->setDescription('Fix gherkin code style')
64
            ->addArgument(
65
                'directory',
66
                InputArgument::REQUIRED,
67
                'Path to find *.feature files'
68
            )
69
            ->addOption(
70
                'align',
71
                null,
72
                InputOption::VALUE_OPTIONAL,
73
                'Side to align statement (right or left). Default right',
74
                'left'
75
            );
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        $align = $input->getArgument('align') === Step::ALIGN_TO_LEFT
84
            ? Step::ALIGN_TO_LEFT
85
            : Step::ALIGN_TO_RIGHT;
86
87
        $directory = $input->getArgument('directory');
88
        $finder    = new Finder();
89
        $finder
90
            ->files()
91
            ->in($directory)
92
            ->name('*.feature');
93
94
        $output->writeln('');
95
        $output->writeln('Finding files on <info>' . $directory . '</info>');
96
        $output->writeln('');
97
98
        /* @var $file \Symfony\Component\Finder\SplFileInfo */
99
        foreach ($finder as $file) {
100
101
            $fileContent = $file->getContents();
102
            $feature     = $this->parser->parse($fileContent);
103
104
            $tagFormatter       = new Tags();
105
            $featureDescription = new FeatureDescription();
106
            $background         = new Background($align);
107
            $scenario           = new Scenario($align);
108
109
            $formatted = $feature->hasTags() ? $tagFormatter->format($feature->getTags()) . PHP_EOL : '';
110
            $formatted .= $featureDescription->format($feature->getTitle(), explode(PHP_EOL, $feature->getDescription())) . PHP_EOL . PHP_EOL;
111
            $formatted .= $feature->hasBackground() ? $background->format($feature->getBackground()) . PHP_EOL . PHP_EOL : '';
112
            $formatted .= $feature->hasScenarios() ? $scenario->format($feature->getScenarios()) : '';
113
114
            $filePointer = $file->openFile('w');
115
            $filePointer->fwrite($formatted);
116
117
            $output->writeln('<info>' . $file->getRealpath() . '</info>');
118
        }
119
    }
120
}
121