These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Config Trait |
||
4 | * |
||
5 | * @package BrightNucleus\Config |
||
6 | * @author Alain Schlesser <[email protected]> |
||
7 | * @license GPL-2.0+ |
||
8 | * @link http://www.brightnucleus.com/ |
||
9 | * @copyright 2016 Alain Schlesser, Bright Nucleus |
||
10 | */ |
||
11 | |||
12 | namespace BrightNucleus\Config; |
||
13 | |||
14 | use Assert; |
||
15 | use BrightNucleus\Exception\RuntimeException; |
||
16 | use Exception; |
||
17 | |||
18 | trait ConfigTrait |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Reference to the Config object. |
||
23 | * |
||
24 | * @since 0.1.2 |
||
25 | * |
||
26 | * @var ConfigInterface |
||
27 | */ |
||
28 | protected $config; |
||
29 | |||
30 | /** |
||
31 | * Process the passed-in configuration file. |
||
32 | * |
||
33 | * @since 0.1.2 |
||
34 | * |
||
35 | * @param ConfigInterface $config The Config to process. |
||
36 | * @param string ... List of keys. |
||
37 | * @throws RuntimeException If the arguments could not be parsed into a Config. |
||
38 | */ |
||
39 | 3 | protected function processConfig(ConfigInterface $config) |
|
40 | { |
||
41 | 3 | if (func_num_args() > 1) { |
|
42 | try { |
||
43 | 2 | $keys = func_get_args(); |
|
44 | 2 | array_shift($keys); |
|
45 | 2 | $config = $config->getSubConfig($keys); |
|
0 ignored issues
–
show
|
|||
46 | 1 | } catch (Exception $exception) { |
|
47 | 1 | throw new RuntimeException( |
|
48 | sprintf( |
||
49 | 1 | _('Could not process the config with the arguments "%1$s".'), |
|
50 | 1 | print_r(func_get_args(), true) |
|
51 | ) |
||
52 | ); |
||
53 | } |
||
54 | } |
||
55 | 2 | $this->config = $config; |
|
56 | 2 | } |
|
57 | |||
58 | /** |
||
59 | * Check whether the Config has a specific key. |
||
60 | * |
||
61 | * To get a value several levels deep, add the keys for each level as a comma-separated list. |
||
62 | * |
||
63 | * @since 0.1.2 |
||
64 | * @since 0.1.5 Accepts list of keys. |
||
65 | * |
||
66 | * @param string ... List of keys. |
||
67 | * @return bool Whether the key is known. |
||
68 | */ |
||
69 | 2 | protected function hasConfigKey() |
|
70 | { |
||
71 | 2 | $keys = func_get_args(); |
|
72 | 2 | Assert\that($keys)->all()->string()->notEmpty(); |
|
73 | |||
74 | 2 | return $this->config->hasKey($keys); |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Get the Config value for a specific key. |
||
79 | * |
||
80 | * To get a value several levels deep, add the keys for each level as a comma-separated list. |
||
81 | * |
||
82 | * @since 0.1.2 |
||
83 | * @since 0.1.5 Accepts list of keys. |
||
84 | * |
||
85 | * @param string ... List of keys. |
||
86 | * @return mixed Value of the key. |
||
87 | */ |
||
88 | 2 | protected function getConfigKey() |
|
89 | { |
||
90 | 2 | $keys = func_get_args(); |
|
91 | 2 | Assert\that($keys)->all()->string()->notEmpty(); |
|
92 | |||
93 | 2 | return $this->config->getKey($keys); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get a (multi-dimensional) array of all the configuration settings. |
||
98 | * |
||
99 | * @since 0.1.4 |
||
100 | * |
||
101 | * @return array All the configuration settings. |
||
102 | */ |
||
103 | 1 | protected function getConfigArray() |
|
104 | { |
||
105 | 1 | return $this->config->getAll(); |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * Get an array of all the keys that are known by the Config. |
||
110 | * |
||
111 | * @since 0.1.2 |
||
112 | * |
||
113 | * @return array Array of strings containing all the keys. |
||
114 | */ |
||
115 | 1 | protected function getConfigKeys() |
|
116 | { |
||
117 | 1 | return $this->config->getKeys(); |
|
118 | } |
||
119 | } |
||
120 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.