| 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 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * PHPSA configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Configuration implements ConfigurationInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Create a configuration from array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param array $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param array $analyzersConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 18 |  |     public function __construct(array $configuration = [], array $analyzersConfiguration = [], $path = "") | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 18 |  |         $this->path = $path; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 18 |  |         $processor = new Processor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 18 |  |         $configTree = $this->getConfigTreeBuilder($analyzersConfiguration); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 18 |  |         $this->configuration = $processor->process( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 18 |  |             $configTree->buildTree(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 18 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Generates the configuration tree. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param array $analyzersConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @return TreeBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 18 |  |     public function getConfigTreeBuilder(array $analyzersConfiguration = []) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 18 |  |         $treeBuilder = new TreeBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 18 |  |         $root = $treeBuilder->root('phpsa'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 18 |  |             ->children() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 18 |  |                 ->booleanNode('blame')->defaultFalse()->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 18 |  |                 ->scalarNode('language_level') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 18 |  |                     ->defaultValue(PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 18 |  |                     ->attribute('example', '5.3') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 18 |  |                     ->attribute('info', 'Will be used to automatically disable the analyzers that require a greater version of PHP.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 18 |  |                 ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 18 |  |                 ->enumNode('parser') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 18 |  |                     ->defaultValue('prefer-7') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 18 |  |                     ->attribute('label', 'Check types of Arguments.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 18 |  |                     ->values([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 18 |  |                         ParserFactory::PREFER_PHP7 => 'prefer-7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 18 |  |                         ParserFactory::PREFER_PHP5 => 'prefer-5', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 18 |  |                         ParserFactory::ONLY_PHP7 => 'only-7', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 18 |  |                         ParserFactory::ONLY_PHP5 => 'only-5' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 18 |  |                     ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 18 |  |                 ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 18 |  |             ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $analyzersConfigRoot = $root | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 18 |  |             ->children() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 18 |  |                 ->arrayNode('analyzers') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 18 |  |                 ->addDefaultsIfNotSet(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 18 |  |         $language_error = (new TreeBuilder())->root('language_error') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 18 |  |             ->info("Contains all compiler notices. Those are raised when PHP with strict error reporting would create at least a Notice message. (mostly experimental)") | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 18 |  |             ->canBeDisabled(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 18 |  |         $analyzersConfigRoot->append($language_error); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 18 |  |         foreach ($analyzersConfiguration as $config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 18 |  |             $analyzersConfigRoot->append($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 18 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 18 |  |         return $treeBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Sets a configuration setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 18 |  |     public function setValue($key, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 18 |  |         $this->configuration[$key] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 18 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * Gets a configuration setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param mixed $default | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 18 |  |     public function getValue($key, $default = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 18 |  |         if (array_key_exists($key, $this->configuration)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 18 |  |             return $this->configuration[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return $default; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * Checks if a configuration setting is set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * @param string $key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 18 |  |     public function valueIsTrue($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 18 |  |         return (bool) $this->configuration[$key]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 141 |  |  |     public function getPath() | 
            
                                                        
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 143 |  |  |         return $this->path; | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                        
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.