Zend\Code\Generator\ClassGenerator::addUse PHP Метод

addUse() публичный Метод

Add a class to "use" classes
public addUse ( string $use, string | null $useAlias = null ) : self
$use string
$useAlias string | null
Результат self
    public function addUse($use, $useAlias = null)
    {
        $this->traitUsageGenerator->addUse($use, $useAlias);
        return $this;
    }

Usage Example

Пример #1
0
 /**
  * ClassDiff constructor.
  *
  * @param string         $currentFilePath
  * @param string         $currentClassName
  * @param ClassGenerator $newClassGenerator
  */
 public function __construct($currentFilePath, $currentClassName, ClassGenerator $newClassGenerator)
 {
     $this->currentFilePath = $currentFilePath;
     $this->currentClassName = $currentClassName;
     $this->currentClassCode = file_get_contents($currentFilePath);
     $this->currentClassGenerator = $this->getGeneratorFromReflection(new ClassReflection($currentClassName));
     $this->newClassGenerator = $newClassGenerator;
     /*
      * PHP Reflections don't take into account use statements, so an entire
      * plugin is needed just for that. //shakes head
      */
     $parser = new Parser(new Lexer());
     $nodes = $parser->parse($this->currentClassCode);
     foreach ($nodes as $node) {
         /** @var $node Namespace_ */
         if (get_class($node) == 'PhpParser\\Node\\Stmt\\Namespace_') {
             /** @var Use_ $stmt */
             foreach ($node->stmts as $stmt) {
                 if (get_class($stmt) == 'PhpParser\\Node\\Stmt\\Use_') {
                     /** @var UseUse $use */
                     foreach ($stmt->uses as $use) {
                         $this->currentClassGenerator->addUse($use->name->toString());
                     }
                 }
             }
         }
     }
     if (in_array($this->currentClassGenerator->getExtendedClass(), $this->currentClassGenerator->getUses())) {
         $extended = new \ReflectionClass($this->currentClassGenerator->getExtendedClass());
         $this->currentClassGenerator->setExtendedClass($extended->getShortName());
     }
 }
All Usage Examples Of Zend\Code\Generator\ClassGenerator::addUse