abstractcheck.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\App\CodeChecker;
  23. abstract class AbstractCheck implements ICheck {
  24. /** @var ICheck */
  25. protected $check;
  26. /**
  27. * @param ICheck $check
  28. */
  29. public function __construct(ICheck $check) {
  30. $this->check = $check;
  31. }
  32. /**
  33. * @param int $errorCode
  34. * @param string $errorObject
  35. * @return string
  36. */
  37. public function getDescription($errorCode, $errorObject) {
  38. switch ($errorCode) {
  39. case CodeChecker::STATIC_CALL_NOT_ALLOWED:
  40. $functions = $this->getLocalFunctions();
  41. $functions = array_change_key_case($functions, CASE_LOWER);
  42. if (isset($functions[$errorObject])) {
  43. return $this->getLocalDescription();
  44. }
  45. // no break;
  46. case CodeChecker::CLASS_EXTENDS_NOT_ALLOWED:
  47. case CodeChecker::CLASS_IMPLEMENTS_NOT_ALLOWED:
  48. case CodeChecker::CLASS_NEW_NOT_ALLOWED:
  49. case CodeChecker::CLASS_USE_NOT_ALLOWED:
  50. $classes = $this->getLocalClasses();
  51. $classes = array_change_key_case($classes, CASE_LOWER);
  52. if (isset($classes[$errorObject])) {
  53. return $this->getLocalDescription();
  54. }
  55. break;
  56. case CodeChecker::CLASS_CONST_FETCH_NOT_ALLOWED:
  57. $constants = $this->getLocalConstants();
  58. $constants = array_change_key_case($constants, CASE_LOWER);
  59. if (isset($constants[$errorObject])) {
  60. return $this->getLocalDescription();
  61. }
  62. break;
  63. case CodeChecker::CLASS_METHOD_CALL_NOT_ALLOWED:
  64. $methods = $this->getLocalMethods();
  65. $methods = array_change_key_case($methods, CASE_LOWER);
  66. if (isset($methods[$errorObject])) {
  67. return $this->getLocalDescription();
  68. }
  69. break;
  70. }
  71. return $this->check->getDescription($errorCode, $errorObject);
  72. }
  73. /**
  74. * @return string
  75. */
  76. abstract protected function getLocalDescription();
  77. /**
  78. * @return array
  79. */
  80. abstract protected function getLocalClasses();
  81. /**
  82. * @return array
  83. */
  84. abstract protected function getLocalConstants();
  85. /**
  86. * @return array
  87. */
  88. abstract protected function getLocalFunctions();
  89. /**
  90. * @return array
  91. */
  92. abstract protected function getLocalMethods();
  93. /**
  94. * @return array E.g.: `'ClassName' => 'oc version',`
  95. */
  96. public function getClasses() {
  97. return array_merge($this->getLocalClasses(), $this->check->getClasses());
  98. }
  99. /**
  100. * @return array E.g.: `'ClassName::CONSTANT_NAME' => 'oc version',`
  101. */
  102. public function getConstants() {
  103. return array_merge($this->getLocalConstants(), $this->check->getConstants());
  104. }
  105. /**
  106. * @return array E.g.: `'functionName' => 'oc version',`
  107. */
  108. public function getFunctions() {
  109. return array_merge($this->getLocalFunctions(), $this->check->getFunctions());
  110. }
  111. /**
  112. * @return array E.g.: `'ClassName::methodName' => 'oc version',`
  113. */
  114. public function getMethods() {
  115. return array_merge($this->getLocalMethods(), $this->check->getMethods());
  116. }
  117. /**
  118. * @return bool
  119. */
  120. public function checkStrongComparisons() {
  121. return $this->check->checkStrongComparisons();
  122. }
  123. }