An open API service indexing awesome lists of open source software.

https://github.com/czproject/phpdepend

Extracts list of dependencies (classes, interfaces & traits) from PHP file or code snippet.
https://github.com/czproject/phpdepend

code-analysis php

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

          

PhpDepend
=========

[![Build Status](https://github.com/czproject/phpdepend/workflows/Build/badge.svg)](https://github.com/czproject/phpdepend/actions)
[![Downloads this Month](https://img.shields.io/packagist/dm/czproject/phpdepend.svg)](https://packagist.org/packages/czproject/phpdepend)
[![Latest Stable Version](https://poser.pugx.org/czproject/phpdepend/v/stable)](https://github.com/czproject/phpdepend/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/czproject/phpdepend/blob/master/license.md)

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

Donate

Installation
------------

[Download a latest package](https://github.com/czproject/phpdepend/releases) or use [Composer](http://getcomposer.org/):

```
composer require czproject/phpdepend
```

PhpDepend requires PHP 8.0 or later and enabled [Tokenizer extension](http://www.php.net/manual/en/book.tokenizer.php) (enabled by default from PHP 4.3.0).

Usage
-----

``` php
$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:
* inherited classes (`extends ParentClass`)
* implemented interfaces (`implements InterfaceA, InterfaceB`)
* used traits (`class MyClass { use Trait; }`)
* classes of created instances (`new Object()`)
* static classes (`StaticClass::staticMethod()`, `StaticClass::$staticProperty`)

Ignored dependencies:
* `self::` - `self` means "this class" → useless (no dependency, class is defined in same file)
* `parent::` - parent class is specified in `extends`
* `static::` - `static` is dynamic-`self` → means "this class", parent or descendant (if exists)

Recognized defined classes (output of `$phpdepend->getClasses()`):
* defined classes (`class MyClass`)
* defined interfaces (`interface MyInterface`)
* defined traits (`trait MyTrait`)

Example
-------

``` php
parse('
say("John");
');

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

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

------------------------------

License: [New BSD License](license.md)

Author: Jan Pecha, https://www.janpecha.cz/