Skip to content

Commit 293016f

Browse files
committed
Merge pull request #44 from PHPCheckstyle/abstract-config
Deduplicate methods into the abstract CheckStyleConfig
2 parents e6d71ab + 0b3afb8 commit 293016f

File tree

3 files changed

+171
-342
lines changed

3 files changed

+171
-342
lines changed

src/PHPCheckstyle/Config/CheckArrayStyleConfig.php

Lines changed: 1 addition & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
*/
99
class CheckArrayStyleConfig extends CheckStyleConfig {
1010

11-
// Array that contains the loaded checks configuration
12-
public $myConfig = array();
13-
1411
/**
1512
* Constructor.
1613
*
@@ -21,160 +18,10 @@ public function __construct($configArray) {
2118

2219
// If the path is a valid file we use it as is
2320
if (is_array($configArray)) {
24-
$this->myConfig = $configArray;
21+
$this->config = $configArray;
2522
} else {
2623
echo "Config must be an array";
2724
exit(0);
2825
}
2926
}
30-
31-
/**
32-
* Return a list of items associed with a test.
33-
*
34-
* @param String $test
35-
* name of the test
36-
* @return array the list of items for this test.
37-
*/
38-
public function getTestItems($test) {
39-
return isset($this->myConfig[$test]['item']) ? $this->myConfig[$test]['item'] : false;
40-
}
41-
42-
/**
43-
* Return a list of items associed with a configuration.
44-
*
45-
* @param String $config
46-
* name of the config
47-
* @return array the list of items for this config.
48-
*/
49-
public function getConfigItems($config) {
50-
if (isset($this->myConfig[$config])) {
51-
return $this->myConfig[$config];
52-
} else {
53-
return array();
54-
}
55-
}
56-
57-
/**
58-
* Return a list of exceptionfor a test.
59-
*
60-
* @param String $test
61-
* name of the test
62-
* @return array the list of exceptions for this test.
63-
*/
64-
public function getTestExceptions($test) {
65-
return isset($this->myConfig[$test]['exception']) ? $this->myConfig[$test]['exception'] : false;
66-
}
67-
68-
/**
69-
* Return a true if the test exist, false otherwise.
70-
*
71-
* @param String $test
72-
* name of the test
73-
* @return Boolean true if test exists.
74-
*/
75-
public function getTest($test) {
76-
return (array_key_exists($test, $this->myConfig));
77-
}
78-
79-
/**
80-
* Return the level of severity of a test.
81-
*
82-
* @param String $test
83-
* name of the test
84-
* @return the level of severity.
85-
*/
86-
public function getTestLevel($test) {
87-
$ret = WARNING;
88-
if (array_key_exists($test, $this->myConfig) && array_key_exists('level', $this->myConfig[$test])) {
89-
$ret = $this->myConfig[$test]['level'];
90-
}
91-
92-
if ($ret !== ERROR && $ret !== IGNORE && $ret !== INFO && $ret !== WARNING) {
93-
echo "Invalid level for test " . $test . " : " . $ret;
94-
$ret = WARNING;
95-
}
96-
97-
return $ret;
98-
}
99-
100-
/**
101-
* Return the regular expression linked to the test.
102-
*
103-
* @param String $test
104-
* name of the test
105-
* @return the regular expression.
106-
*/
107-
public function getTestRegExp($test) {
108-
$ret = "";
109-
if (array_key_exists($test, $this->myConfig) && array_key_exists('regexp', $this->myConfig[$test])) {
110-
$ret = $this->myConfig[$test]['regexp'];
111-
}
112-
113-
return $ret;
114-
}
115-
116-
/**
117-
* Return the list of deprecated method and their replacement.
118-
*
119-
* @param String $test
120-
* name of the test
121-
* @return the list of depecated values.
122-
*/
123-
public function getTestDeprecations($test) {
124-
$ret = "";
125-
if (array_key_exists($test, $this->myConfig)) {
126-
$ret = $this->myConfig[$test];
127-
}
128-
129-
return $ret;
130-
}
131-
132-
/**
133-
* Return the list of aliases and their replacement.
134-
*
135-
* @param String $test
136-
* name of the test
137-
* @return the list of replaced values.
138-
*/
139-
public function getTestAliases($test) {
140-
$ret = "";
141-
if (array_key_exists($test, $this->myConfig)) {
142-
$ret = $this->myConfig[$test];
143-
}
144-
145-
return $ret;
146-
}
147-
148-
/**
149-
* Return the list of replacements.
150-
*
151-
* @param String $test
152-
* name of the test
153-
* @return the list of replaced values.
154-
*/
155-
public function getTestReplacements($test) {
156-
$ret = "";
157-
if (array_key_exists($test, $this->myConfig)) {
158-
$ret = $this->myConfig[$test];
159-
}
160-
161-
return $ret;
162-
}
163-
164-
/**
165-
* Return the value of a property
166-
*
167-
* @param String $test
168-
* name of the test
169-
* @param String $property
170-
* name of the property
171-
* @return the value.
172-
*/
173-
public function getTestProperty($test, $property) {
174-
if (array_key_exists($test, $this->myConfig) && array_key_exists($property, $this->myConfig[$test])) {
175-
return $this->myConfig[$test][$property];
176-
} else {
177-
return false;
178-
}
179-
}
18027
}

src/PHPCheckstyle/Config/CheckStyleConfig.php

Lines changed: 154 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,158 @@
88
* @SuppressWarnings docBlocks
99
*/
1010
abstract class CheckStyleConfig {
11-
abstract public function getTestItems($test);
12-
abstract public function getConfigItems($config);
13-
abstract public function getTestExceptions($test);
14-
abstract public function getTest($test);
15-
abstract public function getTestLevel($test);
16-
abstract public function getTestRegExp($test);
17-
abstract public function getTestDeprecations($test);
18-
abstract public function getTestAliases($test);
19-
abstract public function getTestReplacements($test);
20-
abstract public function getTestProperty($test, $property);
11+
/**
12+
* Stores the check configuration.
13+
* @var array
14+
*/
15+
public $config = array();
16+
17+
/**
18+
* Return a true if the test exist, false otherwise.
19+
*
20+
* @param String $test name of the test
21+
* @return Boolean true if test exists.
22+
*/
23+
public function getTest($test) {
24+
return (array_key_exists(strtolower($test), $this->config));
25+
}
26+
27+
/**
28+
* Return a list of items associed with a test.
29+
*
30+
* @param String $test name of the test
31+
* @return array the list of items for this test.
32+
*/
33+
public function getTestItems($test) {
34+
$test = strtolower($test);
35+
return isset($this->config[$test]['item']) ? $this->config[$test]['item'] : false;
36+
}
37+
38+
/**
39+
* Return a list of exceptionfor a test.
40+
*
41+
* @param String $test name of the test
42+
* @return array the list of exceptions for this test.
43+
*/
44+
public function getTestExceptions($test) {
45+
$test = strtolower($test);
46+
return isset($this->config[$test]['exception']) ? $this->config[$test]['exception'] : false;
47+
}
48+
49+
/**
50+
* Return a list of items associed with a configuration.
51+
*
52+
* @param String $config name of the config
53+
* @return array the list of items for this config.
54+
*/
55+
public function getConfigItems($config) {
56+
$config = strtolower($config);
57+
return isset($this->config[$config]) ? $this->config[$config] : array();
58+
}
59+
60+
/**
61+
* Return the level of severity of a test.
62+
*
63+
* @param String $test name of the test
64+
* @return the level of severity.
65+
*/
66+
public function getTestLevel($test) {
67+
$ret = WARNING;
68+
69+
$test = strtolower($test);
70+
71+
if (array_key_exists($test, $this->config) && array_key_exists('level', $this->config[$test])) {
72+
$ret = $this->config[$test]['level'];
73+
}
74+
75+
$invalidLevels = array(ERROR, IGNORE, INFO, WARNING);
76+
77+
if (!in_array($ret, $invalidLevels)) {
78+
echo "Invalid level for test " . $test . " : " . $ret;
79+
$ret = WARNING;
80+
}
81+
82+
return $ret;
83+
}
84+
85+
/**
86+
* Return the regular expression linked to the test.
87+
*
88+
* @param String $test name of the test
89+
* @return the regular expression.
90+
*/
91+
public function getTestRegExp($test) {
92+
$test = strtolower($test);
93+
$ret = "";
94+
if (array_key_exists($test, $this->config) && array_key_exists('regexp', $this->config[$test])) {
95+
$ret = $this->config[$test]['regexp'];
96+
}
97+
98+
return $ret;
99+
}
100+
101+
/**
102+
* Return the list of deprecated method and their replacement.
103+
*
104+
* @param String $test name of the test
105+
* @return the list of depecated values.
106+
*/
107+
public function getTestDeprecations($test) {
108+
$test = strtolower($test);
109+
$ret = "";
110+
if (array_key_exists($test, $this->config)) {
111+
$ret = $this->config[$test];
112+
}
113+
114+
return $ret;
115+
}
116+
117+
/**
118+
* Return the list of aliases and their replacement.
119+
*
120+
* @param String $test name of the test
121+
* @return the list of replaced values.
122+
*/
123+
public function getTestAliases($test) {
124+
$test = strtolower($test);
125+
$ret = "";
126+
if (array_key_exists($test, $this->config)) {
127+
$ret = $this->config[$test];
128+
}
129+
130+
return $ret;
131+
}
132+
133+
/**
134+
* Return the list of replacements.
135+
*
136+
* @param String $test name of the test
137+
* @return the list of replaced values.
138+
*/
139+
public function getTestReplacements($test) {
140+
$test = strtolower($test);
141+
$ret = "";
142+
if (array_key_exists($test, $this->config)) {
143+
$ret = $this->config[$test];
144+
}
145+
146+
return $ret;
147+
}
148+
149+
/**
150+
* Return the value of a property
151+
*
152+
* @param String $test name of the test
153+
* @param String $property name of the property
154+
* @return the value.
155+
*/
156+
public function getTestProperty($test, $property) {
157+
$test = strtolower($test);
158+
$property = strtolower($property);
159+
if (array_key_exists($test, $this->config) && array_key_exists($property, $this->config[$test])) {
160+
return $this->config[$test][$property];
161+
} else {
162+
return false;
163+
}
164+
}
21165
}

0 commit comments

Comments
 (0)