| 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) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.