| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Symplify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Symplify\PHP7_CodeSniffer\Configuration\OptionResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symplify\PHP7_CodeSniffer\Configuration\ValueNormalizer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symplify\PHP7_CodeSniffer\Contract\Configuration\OptionResolver\OptionResolverInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symplify\PHP7_CodeSniffer\Exception\Configuration\OptionResolver\InvalidSniffCodeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symplify\PHP7_CodeSniffer\Sniff\Naming\SniffNaming; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class SniffsOptionResolver implements OptionResolverInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const NAME = 'sniffs'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 6 |  |     public function getName() : string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 6 |  |         return self::NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 28 | 5 | View Code Duplication |     public function resolve(array $value) : array | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 5 |  |         $optionsResolver = new OptionsResolver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 5 |  |         $optionsResolver->setDefined(self::NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |         $this->setNormalizer($optionsResolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 5 |  |         $this->setSniffsAllowedValues($optionsResolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         $values = $optionsResolver->resolve([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |             self::NAME => $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 5 |  |         return $values[self::NAME]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 5 |  |     private function setNormalizer(OptionsResolver $optionsResolver) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 5 |  |         $optionsResolver->setNormalizer( | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 5 |  |             self::NAME, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             function (OptionsResolver $optionsResolver, array $sniffCodes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 5 |  |                 $sniffCodeToClasses = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 5 |  |                 foreach ($sniffCodes as $sniffCode) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 3 |  |                     $sniffCodeToClasses[$sniffCode] = SniffNaming::guessClassByCode($sniffCode); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 5 |  |                 return $sniffCodeToClasses; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 | 5 |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     private function setSniffsAllowedValues(OptionsResolver $optionsResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |         $optionsResolver->setAllowedValues(self::NAME, function (array $sniffs) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 5 |  |             $sniffs = ValueNormalizer::normalizeCommaSeparatedValues($sniffs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 5 |  |             foreach ($sniffs as $sniff) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |                 if (substr_count($sniff, '.') !== 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     throw new InvalidSniffCodeException(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                         'The specified sniff code "%s" is invalid.' . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         PHP_EOL . | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |                         'Correct format is "StandardName.Category.SniffName".', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         $sniff | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 5 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.