2017 © Pedro Peláez
 

library php-file-parser

parse a php file and get the used namespaces

image

seretos/php-file-parser

parse a php file and get the used namespaces

  • Wednesday, November 2, 2016
  • by Seretos
  • Repository
  • 2 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 6 % Grown

The README.md

PHPFileParser

This library can be used to parse an PHP file and response all used namespaces in this file., (*1)

Installation

Add this repository to your composer.json as below:, (*2)

{
    "require-dev": {
        ...
        "seretos/php-file-parser": "0.3.x-dev"
    }
}

Usage

PHPPackageParser:, (*3)

use PHPFileParser\PHPPackageParser;

$parser = new PHPPackageParser();

$classMap = ['my\\FullNamespace\\class' => __DIR__.'/path/to/file.php'];

$calls = $parser->parse($classMap);     //returns all used classes which not part of the classMap

foreach($calls as $class => $source){
    print_r('class ' .$class . ' is used in ' . $source);
}

PHPFileParser:, (*4)

use Symfony\Component\Finder\Finder;
use PHPFileParser\PHPFileParser;
use PHPFileParser\Parser\NewParser;
use PHPFileParser\Parser\StaticParser;
use PHPFileParser\Parser\ExtendsParser;
use PHPFileParser\Parser\ImplementsParser;
use PHPFileParser\Parser\CatchParser;
use PHPFileParser\Parser\ArgumentParser;
use PHPFileParser\Parser\AnnotationParser;

$finder = new Finder();
$finder->files()->in(__DIR__);

foreach($finder as $file){
    $parser = new PHPFileParser($file);

    $parser->addParser(new NewParser());        // parse new operations like $var = new MyClass();
    $parser->addParser(new StaticParser());     // parse static operations like MyClass::class;
    $parser->addParser(new ExtendsParser());    // parse extended base classes
    $parser->addParser(new ImplementsParser()); // parse implemented class interfaces
    $parser->addParser(new CatchParser());      // parse the catched exception classes
    $parser->addParser(new ArgumentParser());   // parse the type hints in function arguments
    $parser->addParser(new AnnotationParser()); // parse the annotation type declarations
    $parser->addParser(new InstanceOfParser()); // parse instanceof operations

    $parser->parse();               // parse the php file
    $parser->getCalls();            // get an array of used classes (with fully qualified namespace)
    $parser->getNamespaces();       // returns an array with namespaces. every item has an 'use' and an 'alias' key
    $parser->getAllUsedNamespaces() // get all used classes (getCalls and getAnnotationCalls) and remove all doubles
    $parser->getNamespace();        // returns null or the current namespace
}

The Versions

02/11 2016

dev-temp

dev-temp

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

0.3.x-dev

0.3.9999999.9999999-dev

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

dev-master

9999999-dev

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

v0.3.1

0.3.1.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

v0.3.0

0.3.0.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

0.2.x-dev

0.2.9999999.9999999-dev

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

v0.2.5

0.2.5.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

31/10 2016

v0.2.4

0.2.4.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.2.3

0.2.3.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.2.2

0.2.2.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.2.1

0.2.1.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.2.0

0.2.0.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

0.1.x-dev

0.1.9999999.9999999-dev

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.1.1

0.1.1.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen

30/10 2016

v0.1.0

0.1.0.0

parse a php file and get the used namespaces

  Sources   Download

MIT

The Requires

 

The Development Requires

by Arne von Appen