summaryrefslogtreecommitdiffstats
path: root/lib/private/app/codechecker/abstractcheck.php
blob: c1c6524e42f7b68b23ccc143ff809a766fd960e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
 * @author Joas Schilling <nickvergessen@owncloud.com>
 *
 * @copyright Copyright (c) 2015, ownCloud, Inc.
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OC\App\CodeChecker;

abstract class AbstractCheck implements ICheck {
	/** @var ICheck */
	protected $check;

	/**
	 * @param ICheck $check
	 */
	public function __construct(ICheck $check) {
		$this->check = $check;
	}

	/**
	 * @param int $errorCode
	 * @param string $errorObject
	 * @return string
	 */
	public function getDescription($errorCode, $errorObject) {
		switch ($errorCode) {
			case CodeChecker::STATIC_CALL_NOT_ALLOWED:
				$functions = $this->getLocalFunctions();
				$functions = array_change_key_case($functions, CASE_LOWER);
				if (isset($functions[$errorObject])) {
					return $this->getLocalDescription();
				}
			// no break;
			case CodeChecker::CLASS_EXTENDS_NOT_ALLOWED:
			case CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED:
			case CodeChecker::CLASS_NEW_NOT_ALLOWED:
			case CodeChecker::CLASS_USE_NOT_ALLOWED:
				$classes = $this->getLocalClasses();
				$classes = array_change_key_case($classes, CASE_LOWER);
				if (isset($classes[$errorObject])) {
					return $this->getLocalDescription();
				}
			break;

			case CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED:
				$constants = $this->getLocalConstants();
				$constants = array_change_key_case($constants, CASE_LOWER);
				if (isset($constants[$errorObject])) {
					return $this->getLocalDescription();
				}
			break;

			case CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED:
				$methods = $this->getLocalMethods();
				$methods = array_change_key_case($methods, CASE_LOWER);
				if (isset($methods[$errorObject])) {
					return $this->getLocalDescription();
				}
			break;
		}

		return $this->check->getDescription($errorCode, $errorObject);
	}

	/**
	 * @return string
	 */
	abstract protected function getLocalDescription();

	/**
	 * @return array
	 */
	abstract protected function getLocalClasses();

	/**
	 * @return array
	 */
	abstract protected function getLocalConstants();

	/**
	 * @return array
	 */
	abstract protected function getLocalFunctions();

	/**
	 * @return array
	 */
	abstract protected function getLocalMethods();

	/**
	 * @return array E.g.: `'ClassName' => 'oc version',`
	 */
	public function getClasses() {
		return array_merge($this->getLocalClasses(), $this->check->getClasses());
	}

	/**
	 * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',`
	 */
	public function getConstants() {
		return array_merge($this->getLocalConstants(), $this->check->getConstants());
	}

	/**
	 * @return array E.g.: `'functionName' => 'oc version',`
	 */
	public function getFunctions() {
		return array_merge($this->getLocalFunctions(), $this->check->getFunctions());
	}

	/**
	 * @return array E.g.: `'ClassName::methodName' => 'oc version',`
	 */
	public function getMethods() {
		return array_merge($this->getLocalMethods(), $this->check->getMethods());
	}

	/**
	 * @return bool
	 */
	public function checkStrongComparisons() {
		return $this->check->checkStrongComparisons();
	}
}