const INTERFACE_TYPE = 'interface';
const TRAIT_TYPE = 'trait';
- protected $checkTraits;
private $conditionalScopes = 0;
private $atLeastPhp55;
public function __construct()
{
- $this->checkTraits = function_exists('trait_exists');
- $this->atLeastPhp55 = version_compare(PHP_VERSION, '5.5', '>=');
+ $this->atLeastPhp55 = \version_compare(PHP_VERSION, '5.5', '>=');
}
/**
protected function validateClassConstFetchExpression(ClassConstFetch $stmt)
{
// there is no need to check exists for ::class const for php 5.5 or newer
- if (strtolower($stmt->name) === 'class' && $this->atLeastPhp55) {
+ if (\strtolower($stmt->name) === 'class' && $this->atLeastPhp55) {
return;
}
}
if ($errorType !== null) {
- throw $this->createError(sprintf('%s named %s already exists', ucfirst($errorType), $name), $stmt);
+ throw $this->createError(\sprintf('%s named %s already exists', \ucfirst($errorType), $name), $stmt);
}
// Store creation for the rest of this code snippet so we can find local
// issue too
- $this->currentScope[strtolower($name)] = $scopeType;
+ $this->currentScope[\strtolower($name)] = $scopeType;
}
/**
protected function ensureClassExists($name, $stmt)
{
if (!$this->classExists($name)) {
- throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
+ throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
}
}
protected function ensureClassOrInterfaceExists($name, $stmt)
{
if (!$this->classExists($name) && !$this->interfaceExists($name)) {
- throw $this->createError(sprintf('Class \'%s\' not found', $name), $stmt);
+ throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
+ }
+ }
+
+ /**
+ * Ensure that a referenced class _or trait_ exists.
+ *
+ * @throws FatalErrorException
+ *
+ * @param string $name
+ * @param Stmt $stmt
+ */
+ protected function ensureClassOrTraitExists($name, $stmt)
+ {
+ if (!$this->classExists($name) && !$this->traitExists($name)) {
+ throw $this->createError(\sprintf('Class \'%s\' not found', $name), $stmt);
}
}
*/
protected function ensureMethodExists($class, $name, $stmt)
{
- $this->ensureClassExists($class, $stmt);
+ $this->ensureClassOrTraitExists($class, $stmt);
// let's pretend all calls to self, parent and static are valid
- if (in_array(strtolower($class), ['self', 'parent', 'static'])) {
+ if (\in_array(\strtolower($class), ['self', 'parent', 'static'])) {
return;
}
return;
}
- if (!method_exists($class, $name) && !method_exists($class, '__callStatic')) {
- throw $this->createError(sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
+ if (!\method_exists($class, $name) && !\method_exists($class, '__callStatic')) {
+ throw $this->createError(\sprintf('Call to undefined method %s::%s()', $class, $name), $stmt);
}
}
/** @var string $name */
$name = $this->getFullyQualifiedName($interface);
if (!$this->interfaceExists($name)) {
- throw $this->createError(sprintf('Interface \'%s\' not found', $name), $stmt);
+ throw $this->createError(\sprintf('Interface \'%s\' not found', $name), $stmt);
}
}
}
// Give `self`, `static` and `parent` a pass. This will actually let
// some errors through, since we're not checking whether the keyword is
// being used in a class scope.
- if (in_array(strtolower($name), ['self', 'static', 'parent'])) {
+ if (\in_array(\strtolower($name), ['self', 'static', 'parent'])) {
return true;
}
- return class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
+ return \class_exists($name) || $this->findInScope($name) === self::CLASS_TYPE;
}
/**
*/
protected function interfaceExists($name)
{
- return interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
+ return \interface_exists($name) || $this->findInScope($name) === self::INTERFACE_TYPE;
}
/**
*/
protected function traitExists($name)
{
- return $this->checkTraits && (trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE);
+ return \trait_exists($name) || $this->findInScope($name) === self::TRAIT_TYPE;
}
/**
*/
protected function findInScope($name)
{
- $name = strtolower($name);
+ $name = \strtolower($name);
if (isset($this->currentScope[$name])) {
return $this->currentScope[$name];
}