| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author Patsura Dmitry https://github.com/ovr <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace PHPSA; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpParser\ParserFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Config\Definition\Builder\TreeBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Config\Definition\ConfigurationInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Config\Definition\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Configuration implements ConfigurationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Create a configuration from array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param array $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param array $analyzersConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 866 |  |     public function __construct(array $configuration = [], array $analyzersConfiguration = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 866 |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 866 |  |         $configTree = $this->getConfigTreeBuilder($analyzersConfiguration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 866 |  |         $this->configuration = $processor->process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 866 |  |             $configTree->buildTree(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 866 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 866 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Generates the configuration tree. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array $analyzersConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return TreeBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 866 |  |     public function getConfigTreeBuilder(array $analyzersConfiguration = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 866 |  |         $treeBuilder = new TreeBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 866 |  |         $root = $treeBuilder->root('phpsa'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 866 |  |             ->children() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 866 |  |                 ->booleanNode('blame')->defaultFalse()->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 866 |  |                 ->scalarNode('language_level') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 866 |  |                     ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 866 |  |                     ->attribute('example', '5.3') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 866 |  |                     ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 866 |  |                 ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 866 |  |                 ->enumNode('parser') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 866 |  |                     ->defaultValue('prefer-7') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 866 |  |                     ->attribute('label', 'Check types of Arguments.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 866 |  |                     ->values([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 866 |  |                         ParserFactory::PREFER_PHP7 => 'prefer-7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 866 |  |                         ParserFactory::PREFER_PHP5 => 'prefer-5', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 866 |  |                         ParserFactory::ONLY_PHP7 => 'only-7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 866 |  |                         ParserFactory::ONLY_PHP5 => 'only-5' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 866 |  |                     ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 866 |  |                 ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 866 |  |             ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $analyzersConfigRoot = $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 866 |  |             ->children() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 866 |  |                 ->arrayNode('analyzers') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 866 |  |                 ->addDefaultsIfNotSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 866 |  |         foreach ($analyzersConfiguration as $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 1 |  |             $analyzersConfigRoot->append($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 866 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 866 |  |         return $treeBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Sets a configuration setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     public function setValue($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->configuration[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Gets a configuration setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 | 1 |  |     public function getValue($key, $default = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 1 |  |         if (array_key_exists($key, $this->configuration)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 | 1 |  |             return $this->configuration[$key]; | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         return $default; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Checks if a configuration setting is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 39 |  |     public function valueIsTrue($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 39 |  |         return (bool) $this->configuration[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.