Home

Awesome

PhpDepend

Build Status Downloads this Month Latest Stable Version License

Extracts list of dependencies (classes, interfaces & traits) from PHP file or code snippet.

<a href="https://www.janpecha.cz/donate/"><img src="https://buymecoffee.intm.org/img/donate-banner.v1.svg" alt="Donate" height="100"></a>

Installation

Download a latest package or use Composer:

composer require czproject/phpdepend

PhpDepend requires PHP 5.6 or later and enabled Tokenizer extension (enabled by default from PHP 4.3.0).

Usage

$phpdepend = new CzProject\PhpDepend\PhpDepend;

// file parsing
$phpdepend->parseFile('MyClass.php');

// code snippet parsing
$source = file_get_contents('MyClass.php');
$phpdepend->parse($source);

// getting result
$phpdepend->getClasses(); // returns list of defined classes, interfaces & traits
$phpdepend->getDependencies(); // returns list of required classes, interfaces & traits

Recognized dependencies in PHP code:

Ignored dependencies:

Recognized defined classes (output of $phpdepend->getClasses()):

Example

<?php
$phpdepend = new CzProject\PhpDepend\PhpDepend;
$phpdepend->parse('
<?php
	class Greeting implements IGreeting
	{
		public function say($name)
		{
			if (!$name) {
				throw new InvalidArgumentException("Invalid name");
			}
			return "Hello $name";
		}
	}

	$greeting = new Greeting;
	$greeting->say("John");
');

var_dump($phpdepend->getClasses());
/* Output:
array (1) {
	'Greeting'
}
*/

var_dump($phpdepend->getDependencies());
/* Output:
array (3) {
	'IGreeting',
	'InvalidArgumentException',
	'Greeting',
}
*/

License: New BSD License <br>Author: Jan Pecha, https://www.janpecha.cz/