czproject/phpdepend

从PHP文件或代码片段中提取依赖列表(类、接口和特性)。

资助包维护!
其他

v2.0.0 2023-03-31 08:58 UTC

This package is auto-updated.

Last update: 2024-08-30 01:13:52 UTC


README

Build Status Downloads this Month Latest Stable Version License

从PHP文件或代码片段中提取依赖列表(类、接口和特性)。

Donate

安装

下载最新包 或使用 Composer

composer require czproject/phpdepend

PhpDepend 需要 PHP 5.6 或更高版本,并启用了 Tokenizer 扩展(从 PHP 4.3.0 开始默认启用)。

使用方法

$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

PHP代码中识别的依赖

  • 继承的类(extends ParentClass
  • 实现的接口(implements InterfaceA, InterfaceB
  • 使用的特性(class MyClass { use Trait; }
  • 创建实例的类(new Object()
  • 静态类(StaticClass::staticMethod()StaticClass::$staticProperty

忽略的依赖

  • self:: - self 表示 "当前类" → 无用(无依赖,类定义在同一文件中)
  • parent:: - 父类在 extends 中指定
  • static:: - static 是动态的 self → 表示 "当前类",父类或子类(如果存在)

识别的已定义类($phpdepend->getClasses() 的输出)

  • 已定义类(class MyClass
  • 已定义接口(interface MyInterface
  • 已定义特性(trait MyTrait

示例

<?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',
}
*/

许可:新BSD许可
作者:Jan Pecha,https://www.janpecha.cz/