paglliac/dependency-analysis

用于分析PHP项目中静态分析模块依赖的库

v0.1.2 2021-08-03 11:33 UTC

This package is auto-updated.

Last update: 2024-09-29 06:03:04 UTC


README

Build Status

PHP DA是一个用于检查和清晰地支持项目内部依赖的工具。

例如

  • 你的项目有3个根命名空间:Domain、Application、Infrastructure
  • 你想要确保项目中的依赖被定义为图的形式
[
    'dependencies' => [
        '\Domain' => null,
        '\Application' => ['\Domain'],
        '\Infrastructure' => ['\Domain', '\Application']
    ]
];

这意味着来自Domain命名空间的所有类都应该只使用这个命名空间中的类,以及可能配置的供应商(它已配置)。

来自Application的所有类可以调用来自DomainApplication命名空间的类,但不能调用来自Infrastructure的类等。

如果某些类使用的依赖不符合定义的依赖图,你将在报告中得到错误信息

Have been analyzed 4 files
You have dependency problems in 2 files in your project:

Class \Application\TrackingService have errors:
    - Class \Application\TrackingService using class \Infrastructure\ShipImplementation which not satisfy dependency graph

Class \Domain\Cargo have errors:
    - Class \Domain\Cargo using class \Application\TrackingService which not satisfy dependency graph
    - Class \Domain\Cargo using class \Infrastructure\ShipImplementation which not satisfy dependency graph

使用场景

在某些情况下可能很有用,例如

  • 你想将应用程序的一部分提取到单独的服务中,你定义有效的依赖关系并运行php-da来调查工作量
  • 你想要在应用程序中支持低耦合,你定义有效的依赖关系并在CI服务器上运行php-da,对每次合并请求(MR)的更改文件进行操作
  • 你想要让所有开发者都能看到应用程序的结构变化,现在这些变化在php-da配置中是可见的

快速开始

使用composer安装库

composer require paglliac/dependency-analysis

配置

将配置文件config.php添加到项目的根目录

return [
    /**
     * REQUIRED
     * Dependencies Graph
     *
     * Description of valid dependencies in project
     *
     * This config means
     *
     * Package (every class in namespace) \Domain can use only classes from namespace \Domain or vendor dependencies
     * Package \Application can use only classes from namespaces \Domain, \Application or vendor dependencies
     * Package \Infrastructure can use only classes from namespaces \Domain, \Application, \Infrastructure or vendor dependencies
     */
    'dependencies' => [
        '\Domain' => null,
        '\Application' => ['\Domain'],
        '\Infrastructure' => ['\Domain', '\Application']
    ],

    /**
     * REQUIRED
     * Source path where dependencies will be analyzed
     */
    'path' => __DIR__,

    /**
     * OPTIONAL
     *
     * Make available to use vendor dependencies in whole project
     *
     * true - all project classes can use vendor dependencies
     * false - all project can not use vendor dependencies
     */
    'skip_vendor_dir' => true,

    /**
     * OPTIONAL
     * Flag that define how to do when some files placed in namespaces not presented in Dependencies Graph
     *
     * true - mark class as having incorrect dependencies
     * false - skip this file
     *
     * For example, in directory we have namespace \SomeNamespace with class \SomeNamespace\SomeClass
     * if flag is true, it will be marked as incorrect file, if flag is true, this file wil be marked as correct
     */
    'fail_on_non_presented_namespace' => false,

    /**
     * OPTIONAL
     * Flag for php parser, correct values:
     *
     * PhpParser\ParserFactory::PREFER_PHP7 - 1 (default)
     * PhpParser\ParserFactory::PREFER_PHP5 - 2
     * PhpParser\ParserFactory::ONLY_PHP7 - 3
     * PhpParser\ParserFactory::ONLY_PHP5 - 4
     */
    'php_version' => PhpParser\ParserFactory::PREFER_PHP7,

    /**
     * OPTIONAL
     * 
     * List of allowed files extensions, all files with other extensions will be skipped from analysis
     * 
     * Default - ['php']
     */
    'allowed_extensions' => ['php']
];

运行

运行依赖验证

/vendor/bin/php-da -c config.php [files filter]

选项

  • -c--config是必须的选项,指定配置文件的相对路径

参数

  • [文件过滤器]分析文件列表,在CI中使用时与--diff结合很有用

输出示例

Have been analyzed 4 files
You have dependency problems in 2 files in your project:

Class \Application\TrackingService have errors:
    - Class \Application\TrackingService using class \Infrastructure\ShipImplementation which not satisfy dependency graph

Class \Domain\Cargo have errors:
    - Class \Domain\Cargo using class \Application\TrackingService which not satisfy dependency graph
    - Class \Domain\Cargo using class \Infrastructure\ShipImplementation which not satisfy dependency graph