| 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; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symplify\PHP7_CodeSniffer\Exception\Configuration\InvalidSniffCodeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symplify\PHP7_CodeSniffer\Exception\Configuration\SourceNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symplify\PHP7_CodeSniffer\Exception\Configuration\StandardNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symplify\PHP7_CodeSniffer\Standard\StandardFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | final class OptionsResolverFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var StandardFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $standardFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |     public function __construct(StandardFinder $standardFinder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |         $this->standardFinder = $standardFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 1 |  |     public function create() : OptionsResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |         $optionsResolver = new OptionsResolver(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $optionsResolver->setDefined(['standards', 'sniffs', 'source']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         $this->setAllowedValues($optionsResolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 1 |  |         $this->setNormalizers($optionsResolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         return $optionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 1 |  |     private function setAllowedValues(OptionsResolver $optionsResolver) | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $optionsResolver->setAllowedValues('standards', function (array $standards) { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 1 |  |             $standards = $this->normalizeCommaSeparatedValues($standards); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |             $availableStandards = $this->standardFinder->getStandards(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 | 1 |  |             foreach ($standards as $standardName) { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 1 |  |                 if (!array_key_exists($standardName, $availableStandards)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 1 |  |                     throw new StandardNotFoundException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 47 | 1 |  |                         'Standard "%s" is not supported. Pick one of: %s', | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                         $standardName, | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 1 |  |                         implode(array_keys($availableStandards), ', ') | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 1 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $optionsResolver->setAllowedValues('sniffs', function (array $sniffs) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $sniffs = $this->normalizeCommaSeparatedValues($sniffs); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |             foreach ($sniffs as $sniff) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                 if (substr_count($sniff, '.') !== 2) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                     throw new InvalidSniffCodeException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                         'The specified sniff code "%s" is invalid.'. | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                         PHP_EOL. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |                         'Correct format is "StandardName.Category.SniffName".', | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |                         $sniff | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         $optionsResolver->setAllowedValues('source', function (array $source) { | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             foreach ($source as $singleSource) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 if (!file_exists($singleSource)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |                     throw new SourceNotFoundException(sprintf( | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |                         'Source "%s" does not exist.', | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |                         $singleSource | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |                     )); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |             return true; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |     private function setNormalizers(OptionsResolver $optionsResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |         $optionsResolver->setNormalizer( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |             'standards', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |             function (OptionsResolver $optionsResolver, array $standardNames) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 $standardNames = $this->normalizeCommaSeparatedValues($standardNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 return $this->standardFinder->getRulesetPathsForStandardNames($standardNames); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  |     private function normalizeCommaSeparatedValues(array $values) : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 1 |  |         $newValues = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 1 |  |         foreach ($values as $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |             $newValues = array_merge($newValues, explode(',', $value)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         return $newValues; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |