1 | <?php |
||
15 | class ConfigManager extends SiteMetaManager |
||
16 | { |
||
17 | const RAW_STRINGS = [ |
||
18 | // '__', '_n', '_x', 'esc_attr__', 'esc_html__', 'sprintf', |
||
1 ignored issue
–
show
|
|||
19 | ]; |
||
20 | |||
21 | public $compiled; |
||
22 | |||
23 | public $parseError = false; |
||
24 | |||
25 | /** |
||
26 | * @var Application |
||
27 | */ |
||
28 | protected $app; |
||
29 | |||
30 | public function __construct( Application $app ) |
||
31 | { |
||
32 | $this->app = $app; |
||
33 | $this->options = $this->buildConfig(); |
||
34 | $this->compiled = $this->compile(); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | public function buildConfig() |
||
41 | { |
||
42 | $yamlFile = $this->getYamlFile(); |
||
43 | $yaml = $this->normalizeYamlValues( $this->normalize( |
||
44 | $this->parseYaml( file_get_contents( $yamlFile ), $yamlFile ) |
||
45 | )); |
||
46 | if( !$yaml['disable_config'] ) { |
||
47 | $config = array_filter( (array) get_option( Config::id(), [] )); |
||
48 | } |
||
49 | return empty( $config ) |
||
50 | ? $this->setTimestamp( $yaml, filemtime( $yamlFile )) |
||
51 | : $this->normalizeYamlValues( $this->normalize( $config )); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return object |
||
56 | */ |
||
57 | public function compile() |
||
58 | { |
||
59 | $configFile = $this->getCompileDestination(); |
||
60 | if( $this->shouldCompile( $configFile )) { |
||
61 | $config = $this->normalizeArray( $this->options ); |
||
62 | if( $this->parseError ) { |
||
63 | return (object) $config; |
||
64 | } |
||
65 | file_put_contents( $configFile, sprintf( '<?php // DO NOT MODIFY THIS FILE DIRECTLY!%sreturn (object) %s;', |
||
66 | PHP_EOL, |
||
67 | $this->parseRawStrings( var_export( $this->setTimestamp( $config ), true )) |
||
68 | )); |
||
69 | } |
||
70 | return include $configFile; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | public function convertArrayToYaml( array $array ) |
||
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getCompileDestination( $filename = 'pollux-config.php' ) |
||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | public function getYamlFile() |
||
115 | |||
116 | /** |
||
117 | * @return array |
||
118 | */ |
||
119 | public function normalizeArray( array $array ) |
||
131 | |||
132 | /** |
||
133 | * @return array |
||
134 | */ |
||
135 | public function normalizeYamlValues( array $array ) |
||
143 | |||
144 | /** |
||
145 | * @return array |
||
146 | */ |
||
147 | public function setTimestamp( array $config, $timestamp = null ) |
||
153 | |||
154 | /** |
||
155 | * @return string|null |
||
156 | */ |
||
157 | protected function dumpYaml( array $array ) |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | protected function normalize( array $config ) |
||
180 | |||
181 | /** |
||
182 | * @param string $configString |
||
183 | * @return string |
||
184 | * @todo only allow raw strings when we can parse them properly without using eval() |
||
185 | */ |
||
186 | protected function parseRawStrings( $configString ) |
||
199 | |||
200 | /** |
||
201 | * @link http://api.symfony.com/3.2/Symfony/Component/Yaml/Exception/ParseException.html |
||
202 | * @return array |
||
203 | */ |
||
204 | protected function parseYaml( $value, $file = null ) |
||
224 | |||
225 | /** |
||
226 | * @param string $configFile |
||
227 | * @return bool |
||
228 | */ |
||
229 | protected function shouldCompile( $configFile ) |
||
240 | } |
||
241 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.