CodeSniffer嗅探用于为PHP代码生成依赖图? [英] CodeSniffer sniff for generating dependency graphs for PHP code?

查看:27
本文介绍了CodeSniffer嗅探用于为PHP代码生成依赖图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:我有兴趣生成一个 DOT 格式 中的类依赖关系描述PHP程序.

GOAL: I'm interested in generating a DOT Format description of the class dependencies in a PHP program.

想法:在 PHP 源代码中编写一个可以检测(并发出 DOT 记录)的 CodeSniffer嗅探器"应该不难:

IDEA: It shouldn't be hard to write a CodeSniffer "sniff" that can detect (and emit DOT records for) the following patterns in PHP source:

class SomeClassName extends BasicClassName {  // SomeClassName refers to BasicClassName
...
    new OtherClassName();            // SomeClassName refers to OtherClassName
    ThisClassName::some_method();    // SomeClassName refers to ThisClassName
    ThatClassName::$some_member;     // SomeClassName refers to ThatClassName
    RandomClassName::some_constant;  // SomeClassName refers to RandomClassName
...
}

但我还没有发现任何已发布的嗅探来发出此信息(以及任何其他表明我可能遗漏的真实"类依赖关系的模式).

But I haven't found any published sniffs to emit this information (and any other patterns indicating a "real" class dependency relationship that I may have missed).

注意:我特别关心 PHP 的 include() 和 require() 语句(我不相信其行为甚至是明确定义的).对于这个问题,我们假设所有 PHP 类解析都是通过 autoloading,并且我们希望仅使用静态代码分析来构建类依赖关系图.

NOTE: I specifically do not care about PHP's include() and require() statements (whose behavior I'm not convinced is even well-defined). For the purposes of this question let's assume that all PHP class resolution is handled via autoloading, and that we're looking to use only static code analysis to build the class dependency diagram.

不幸的是,我看不到处理以下问题的通用方法:

Unfortunately, I see no general way to deal with the following:

class ThatClassName {
...
    function generateClassName() {
        // something too complicated to analyze statically...
    }

    function foo() {
        $name = $this->generateClassName();
        $instance = new $name; // ThatClassName refers to ... what?
        ...
    }
...
}

但当然,通过显示 ThatClassName 与 generateClassName() 方法的依赖关系,当然可以在依赖关系图中表示这种情况 - 可能用括号显示,以使方法名称易于与类名称区分开来.建立一个约定,即任何动态生成类名的方法都必须包含一个 注释(在相关的注释中),它指示可能生成的每个类名 - 这些记录的动态依赖项"可以自动包含在依赖关系图中.

But of course it would be possible to represent this scenario in a dependency graph by showing ThatClassName with a dependency on the generateClassName() method - perhaps shown with parens to make the method name easily distinguishable from a class name. And it probably wouldn't be a bad idea to establish a convention whereby any method which generates a class name dynamically must contain an annotation (in the associated comment) which indicates every class name which might possibly be generated - these "documented dynamic dependencies" could then be automatically included in the dependency graph.

推荐答案

我为此编写了一个工具:PhpDependencyAnalysis.

I wrote a tool for this: PhpDependencyAnalysis.

这是一个基于命名空间的面向对象的 PHP 项目 (>= 5.3.3) 的可扩展静态代码分析.它在可自定义的级别上创建依赖关系图,例如在包级别或类级别.因此,它通常可用于声明依赖关系,但也可用于根据关注点分离、迪米特法则和非循环依赖原则的遵守情况来检测分层架构中的层之间的违规.您也可以将输出格式更改为 DOT.

This is an extandable static code analysis for object-oriented PHP-Projects (>= 5.3.3) based on namespaces. It creates dependency graphs on customizable levels, e.g. on package-level or on class-level. Thus, it's usable to declare dependencies in general, but it's also usable to perform a detection of violations between layers in a tiered architecture according to compliance with Separation of Concerns, Law of Demeter and Acyclic Dependencies Principle. You can also change the output format to DOT.

只需查看 GitHub 上的 PhpDependencyAnalysis.

这篇关于CodeSniffer嗅探用于为PHP代码生成依赖图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆