/** * @param $name * @param boolean|false $inherit * @return ClassMethod */ public function getMethod($name, $inherit = false) { if (isset($this->methods[$name])) { return $this->methods[$name]; } return $inherit && $this->extendsClassDefinition && $this->extendsClassDefinition->getMethod($name, true); }
/** * @param string $name * @param bool $inherit * @return Node\Stmt\Property */ public function getPropertyStatement($name, $inherit = false) { if (isset($this->propertyStatements[$name])) { return $this->propertyStatements[$name]; } if ($inherit && $this->extendsClassDefinition) { return $this->extendsClassDefinition->getPropertyStatement($name, true); } return null; }
/** * @param $type * @param $message * @param \PhpParser\NodeAbstract $expr * @return bool */ public function notice($type, $message, \PhpParser\NodeAbstract $expr) { $code = file($this->scope->getFilepath()); $this->output->writeln('<comment>Notice: ' . $message . " in {$this->scope->getFilepath()} on {$expr->getLine()} [{$type}]</comment>"); $this->output->writeln(''); if ($this->application->getConfiguration()->valueIsTrue('blame')) { exec("git blame --show-email -L {$expr->getLine()},{$expr->getLine()} " . $this->scope->getFilepath(), $result); if ($result && isset($result[0])) { $result[0] = trim($result[0]); $this->output->writeln("<comment>\t {$result[0]}</comment>"); } } else { $code = trim($code[$expr->getLine() - 1]); $this->output->writeln("<comment>\t {$code} </comment>"); } $this->output->writeln(''); unset($code); return true; }
/** * @param ClassDefinition $class */ public function addClass(ClassDefinition $class) { $this->classes[implode('\\', [$class->getNamespace(), $class->getName()])] = $class; }
/** * @param Node\Stmt|Node\Stmt[] $topStatement * @param AliasManager $aliasManager * @param string $filepath */ protected function parseTopDefinitions($topStatement, AliasManager $aliasManager, $filepath) { foreach ($topStatement as $statement) { if ($statement instanceof Node\Stmt\Use_) { if (count($statement->uses) > 0) { foreach ($statement->uses as $use) { $aliasManager->add($use->name->parts); } } } elseif ($statement instanceof Node\Stmt\GroupUse) { if (count($statement->uses) > 0) { foreach ($statement->uses as $use) { $aliasManager->add($statement->prefix . $use->name->parts); } } } elseif ($statement instanceof Node\Stmt\Trait_) { $definition = new TraitDefinition($statement->name, $statement); $definition->setFilepath($filepath); $definition->setNamespace($aliasManager->getNamespace()); $definition->precompile(); $this->compiler->addTrait($definition); } elseif ($statement instanceof Node\Stmt\Class_) { $definition = new ClassDefinition($statement->name, $statement, $statement->type); $definition->setFilepath($filepath); $definition->setNamespace($aliasManager->getNamespace()); if ($statement->extends) { $definition->setExtendsClass($statement->extends->toString()); } if ($statement->implements) { foreach ($statement->implements as $interface) { $definition->addInterface($interface->toString()); } } foreach ($statement->stmts as $stmt) { if ($stmt instanceof Node\Stmt\ClassMethod) { $definition->addMethod(new ClassMethod($stmt->name, $stmt, $stmt->type)); } elseif ($stmt instanceof Node\Stmt\Property) { $definition->addProperty($stmt); } elseif ($stmt instanceof Node\Stmt\TraitUse) { foreach ($stmt->traits as $traitPart) { $traitDefinition = $this->compiler->getTrait($traitPart->toString()); if ($traitDefinition) { $definition->mergeTrait($traitDefinition, $stmt->adaptations); } } } elseif ($stmt instanceof Node\Stmt\ClassConst) { $definition->addConst($stmt); } } $this->compiler->addClass($definition); } elseif ($statement instanceof Node\Stmt\Function_) { $definition = new FunctionDefinition($statement->name, $statement); $definition->setFilepath($filepath); $definition->setNamespace($aliasManager->getNamespace()); $this->compiler->addFunction($definition); } } }
/** * @param ClassDefinition $class */ public function addClass(ClassDefinition $class) { $this->classes[$class->getName()] = $class; }
protected function parseTopDefinitions($topStatement, $aliasManager, $filepath, $namespace) { foreach ($topStatement as $statement) { if ($statement instanceof Node\Stmt\Use_) { if (!empty($statement->uses)) { foreach ($statement->uses as $use) { $aliasManager->add($use->name->parts); } } } elseif ($statement instanceof Node\Stmt\Class_) { $definition = new ClassDefinition($statement->name, $statement->type); $definition->setFilepath($filepath); $definition->setNamespace($namespace); foreach ($statement->stmts as $stmt) { if ($stmt instanceof Node\Stmt\ClassMethod) { $method = new ClassMethod($stmt->name, $stmt, $stmt->type); $definition->addMethod($method); } elseif ($stmt instanceof Node\Stmt\Property) { $definition->addProperty($stmt); } elseif ($stmt instanceof Node\Stmt\ClassConst) { $definition->addConst($stmt); } } $this->getCompiler()->addClass($definition); } elseif ($statement instanceof Node\Stmt\Function_) { $definition = new FunctionDefinition($statement->name, $statement); $definition->setFilepath($filepath); $definition->setNamespace($namespace); $this->getCompiler()->addFunction($definition); } } }
/** * @param string $filepath * @param Parser $parser * @param Context $context */ protected function parserFile($filepath, Parser $parser, Context $context) { try { if (!is_readable($filepath)) { throw new RuntimeException('File ' . $filepath . ' is not readable'); } $code = file_get_contents($filepath); $stmts = $parser->parse($code); /** * Step 1 Precompile */ if ($stmts[0] instanceof Node\Stmt\Namespace_) { $stmts = $stmts[0]->stmts; } foreach ($stmts as $st) { if ($st instanceof Node\Stmt\Class_) { $classDefintion = new ClassDefinition($st->name); $classDefintion->setFilepath($filepath); foreach ($st->stmts as $st) { if ($st instanceof Node\Stmt\ClassMethod) { $method = new ClassMethod($st->name, $st->stmts, $st->type, $st); $classDefintion->addMethod($method); } elseif ($st instanceof Node\Stmt\Property) { $classDefintion->addProperty($st); } elseif ($st instanceof Node\Stmt\ClassConst) { $classDefintion->addConst($st); } } $this->classes[] = $classDefintion; } } $context->clear(); } catch (\PhpParser\Error $e) { $context->sytaxError($e, $filepath); } catch (Exception $e) { $context->output->writeln("<error>{$e->getMessage()}</error>"); } }