PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/psy/psysh/src/Psy/CodeCleaner/ValidClassNamePass.php

https://gitlab.com/judielsm/Handora
PHP | 323 lines | 151 code | 29 blank | 143 comment | 19 complexity | f1420960fa6375fb0031aa5cc4c30091 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell
  4. *
  5. * (c) 2012-2014 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy\CodeCleaner;
  11. use PhpParser\Node;
  12. use PhpParser\Node\Expr;
  13. use PhpParser\Node\Expr\ClassConstFetch;
  14. use PhpParser\Node\Expr\New_ as NewExpr;
  15. use PhpParser\Node\Expr\StaticCall;
  16. use PhpParser\Node\Stmt;
  17. use PhpParser\Node\Stmt\Class_ as ClassStmt;
  18. use PhpParser\Node\Stmt\Interface_ as InterfaceStmt;
  19. use PhpParser\Node\Stmt\Trait_ as TraitStmt;
  20. use Psy\Exception\FatalErrorException;
  21. /**
  22. * Validate that classes exist.
  23. *
  24. * This pass throws a FatalErrorException rather than letting PHP run
  25. * headfirst into a real fatal error and die.
  26. */
  27. class ValidClassNamePass extends NamespaceAwarePass
  28. {
  29. const CLASS_TYPE = 'class';
  30. const INTERFACE_TYPE = 'interface';
  31. const TRAIT_TYPE = 'trait';
  32. protected $checkTraits;
  33. public function __construct()
  34. {
  35. $this->checkTraits = function_exists('trait_exists');
  36. }
  37. /**
  38. * Validate class, interface and trait statements, and `new` expressions.
  39. *
  40. * @throws FatalErrorException if a class, interface or trait is referenced which does not exist.
  41. * @throws FatalErrorException if a class extends something that is not a class.
  42. * @throws FatalErrorException if a class implements something that is not an interface.
  43. * @throws FatalErrorException if an interface extends something that is not an interface.
  44. * @throws FatalErrorException if a class, interface or trait redefines an existing class, interface or trait name.
  45. *
  46. * @param Node $node
  47. */
  48. public function leaveNode(Node $node)
  49. {
  50. if ($node instanceof ClassStmt) {
  51. $this->validateClassStatement($node);
  52. } elseif ($node instanceof InterfaceStmt) {
  53. $this->validateInterfaceStatement($node);
  54. } elseif ($node instanceof TraitStmt) {
  55. $this->validateTraitStatement($node);
  56. } elseif ($node instanceof NewExpr) {
  57. $this->validateNewExpression($node);
  58. } elseif ($node instanceof ClassConstFetch) {
  59. $this->validateClassConstFetchExpression($node);
  60. } elseif ($node instanceof StaticCall) {
  61. $this->validateStaticCallExpression($node);
  62. }
  63. }
  64. /**
  65. * Validate a class definition statement.
  66. *
  67. * @param ClassStmt $stmt
  68. */
  69. protected function validateClassStatement(ClassStmt $stmt)
  70. {
  71. $this->ensureCanDefine($stmt);
  72. if (isset($stmt->extends)) {
  73. $this->ensureClassExists($this->getFullyQualifiedName($stmt->extends), $stmt);
  74. }
  75. $this->ensureInterfacesExist($stmt->implements, $stmt);
  76. }
  77. /**
  78. * Validate an interface definition statement.
  79. *
  80. * @param InterfaceStmt $stmt
  81. */
  82. protected function validateInterfaceStatement(InterfaceStmt $stmt)
  83. {
  84. $this->ensureCanDefine($stmt);
  85. $this->ensureInterfacesExist($stmt->extends, $stmt);
  86. }
  87. /**
  88. * Validate a trait definition statement.
  89. *
  90. * @param TraitStmt $stmt
  91. */
  92. protected function validateTraitStatement(TraitStmt $stmt)
  93. {
  94. $this->ensureCanDefine($stmt);
  95. }
  96. /**
  97. * Validate a `new` expression.
  98. *
  99. * @param NewExpr $stmt
  100. */
  101. protected function validateNewExpression(NewExpr $stmt)
  102. {
  103. // if class name is an expression, give it a pass for now
  104. if (!$stmt->class instanceof Expr) {
  105. $this->ensureClassExists($this->getFullyQualifiedName($stmt->class), $stmt);
  106. }
  107. }
  108. /**
  109. * Validate a class constant fetch expression's class.
  110. *
  111. * @param ClassConstFetch $stmt
  112. */
  113. protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
  114. {
  115. // if class name is an expression, give it a pass for now
  116. if (!$stmt->class instanceof Expr) {
  117. $this->ensureClassExists($this->getFullyQualifiedName($stmt->class), $stmt);
  118. }
  119. }
  120. /**
  121. * Validate a class constant fetch expression's class.
  122. *
  123. * @param StaticCall $stmt
  124. */
  125. protected function validateStaticCallExpression(StaticCall $stmt)
  126. {
  127. // if class name is an expression, give it a pass for now
  128. if (!$stmt->class instanceof Expr) {
  129. $this->ensureMethodExists($this->getFullyQualifiedName($stmt->class), $stmt->name, $stmt);
  130. }
  131. }
  132. /**
  133. * Ensure that no class, interface or trait name collides with a new definition.
  134. *
  135. * @throws FatalErrorException
  136. *
  137. * @param Stmt $stmt
  138. */
  139. protected function ensureCanDefine(Stmt $stmt)
  140. {
  141. $name = $this->getFullyQualifiedName($stmt->name);
  142. // check for name collisions
  143. $errorType = null;
  144. if ($this->classExists($name)) {
  145. $errorType = self::CLASS_TYPE;
  146. } elseif ($this->interfaceExists($name)) {
  147. $errorType = self::INTERFACE_TYPE;
  148. } elseif ($this->traitExists($name)) {
  149. $errorType = self::TRAIT_TYPE;
  150. }
  151. if ($errorType !== null) {
  152. throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt);
  153. }
  154. // Store creation for the rest of this code snippet so we can find local
  155. // issue too
  156. $this->currentScope[strtolower($name)] = $this->getScopeType($stmt);
  157. }
  158. /**
  159. * Ensure that a referenced class exists.
  160. *
  161. * @throws FatalErrorException
  162. *
  163. * @param string $name
  164. * @param Stmt $stmt
  165. */
  166. protected function ensureClassExists($name, $stmt)
  167. {
  168. if (!$this->classExists($name)) {
  169. throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
  170. }
  171. }
  172. /**
  173. * Ensure that a statically called method exists.
  174. *
  175. * @throws FatalErrorException
  176. *
  177. * @param string $class
  178. * @param string $name
  179. * @param Stmt $stmt
  180. */
  181. protected function ensureMethodExists($class, $name, $stmt)
  182. {
  183. $this->ensureClassExists($class, $stmt);
  184. // if method name is an expression, give it a pass for now
  185. if ($name instanceof Expr) {
  186. return;
  187. }
  188. if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) {
  189. throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
  190. }
  191. }
  192. /**
  193. * Ensure that a referenced interface exists.
  194. *
  195. * @throws FatalErrorException
  196. *
  197. * @param $interfaces
  198. * @param Stmt $stmt
  199. */
  200. protected function ensureInterfacesExist($interfaces, $stmt)
  201. {
  202. foreach ($interfaces as $interface) {
  203. /** @var string $name */
  204. $name = $this->getFullyQualifiedName($interface);
  205. if (!$this->interfaceExists($name)) {
  206. throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt);
  207. }
  208. }
  209. }
  210. /**
  211. * Get a symbol type key for storing in the scope name cache.
  212. *
  213. * @param Stmt $stmt
  214. *
  215. * @return string
  216. */
  217. protected function getScopeType(Stmt $stmt)
  218. {
  219. if ($stmt instanceof ClassStmt) {
  220. return self::CLASS_TYPE;
  221. } elseif ($stmt instanceof InterfaceStmt) {
  222. return self::INTERFACE_TYPE;
  223. } elseif ($stmt instanceof TraitStmt) {
  224. return self::TRAIT_TYPE;
  225. }
  226. }
  227. /**
  228. * Check whether a class exists, or has been defined in the current code snippet.
  229. *
  230. * Gives `self`, `static` and `parent` a free pass.
  231. *
  232. * @param string $name
  233. *
  234. * @return bool
  235. */
  236. protected function classExists($name)
  237. {
  238. // Give `self`, `static` and `parent` a pass. This will actually let
  239. // some errors through, since we're not checking whether the keyword is
  240. // being used in a class scope.
  241. if (in_array(strtolower($name), array('self', 'static', 'parent'))) {
  242. return true;
  243. }
  244. return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
  245. }
  246. /**
  247. * Check whether an interface exists, or has been defined in the current code snippet.
  248. *
  249. * @param string $name
  250. *
  251. * @return bool
  252. */
  253. protected function interfaceExists($name)
  254. {
  255. return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
  256. }
  257. /**
  258. * Check whether a trait exists, or has been defined in the current code snippet.
  259. *
  260. * @param string $name
  261. *
  262. * @return bool
  263. */
  264. protected function traitExists($name)
  265. {
  266. return $this->checkTraits && (trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE);
  267. }
  268. /**
  269. * Find a symbol in the current code snippet scope.
  270. *
  271. * @param string $name
  272. *
  273. * @return string|null
  274. */
  275. protected function findInScope($name)
  276. {
  277. $name = strtolower($name);
  278. if (isset($this->currentScope[$name])) {
  279. return $this->currentScope[$name];
  280. }
  281. }
  282. /**
  283. * Error creation factory.
  284. *
  285. * @param string $msg
  286. * @param Stmt $stmt
  287. *
  288. * @return FatalErrorException
  289. */
  290. protected function createError($msg, $stmt)
  291. {
  292. return new FatalErrorException($msg, 0, 1, null, $stmt->getLine());
  293. }
  294. }