6

我正在研究一些 php 库。我想跟踪类库接口的变化。

  • 是的,我正在使用 GIT。但是 diff 产生的信息比我需要的多。
  • 我试图将两个版本与 phpDocumentor 进行比较。

    phpdoc 项目:parse -d 文件夹

生成具有项目界面结构的xml文件。我可以将此 xml 文件与另一个文件进行比较。但是这些包含的信息比我想要的要多。像行号,文件哈希等等。

因此,我想比较两个提交、分支甚至分叉,并找出它们接口的差异。

比较示例:http: //pastebin.com/1H61dJBT

知道何时更改主要版本也很重要。

进行不兼容的 API 更改时的主要版本...

http://semver.org/spec/v2.0.0.html

4

2 回答 2

2

我认为您想围绕PHP Smart Differencer构建工作流程

可以在此处查看PHP Smart Differencer 的结果示例。此页面包含 SD 的 PHP Smart Differencer 工具在应用于原始文件和同一文件的更新版本时生成的输出示例。

如果您真的想自己动手,您可以围绕PHP-Parser构建它,这可能会给您带来更高的精度,但是您需要生成自己的比较算法,其中 SmartDifferencer 的工具已经内置了该算法。

这些中的任何一个都会为您提供您正在寻找的东西。

于 2013-03-28T18:52:13.593 回答
2

普通差异报告文本行方面的差异。软件是根据代码结构定义的。源代码作为文本行和源作为结构之间的不匹配使 diff 的输出难以理解。

您可以将接口定义与 Semantic Designs(我公司的)SmartDifferencer 进行比较

这报告了对代码结构(而不​​是行)的最小编辑,以将一段 (PHP) 代码转换为另一段。接口函数名称、插入或删除参数的更改变得非常明显。换行符不相关,不会影响 SmartDifferencer 的结果;除非您坚持,否则评论也不是。(SmartDifferencer 不限于 PHP;有多种语言的版本)。

编辑:OP 想知道 SmartDifferencer 对接口更改的特定示例(PHP 文件之前和之后)做了什么。下面是他的两个示例文件,它们的 SmartDifferencer 输出和 Unix-diff 输出。我注意到这些非常小的示例文件有很多变化。

第一个界面:

<?php
// ----- first version -----
namespace vendor\package;

class someClass
{
private $c;
public function __construct($a, $b)
    {
    $c = $a + $b;
    }
/**
 * @return string
 */
public function returnC()
    {
    return $this->c;
    }
public function saySomething()
    {
    echo 'something';
    }
}

修改接口文件

<?php
// ----- second version -----
namespace vendor\package;

class someClass
{
private $a, $b;

public function __construct($a, $b)
    {
    $this->a = $a;
    $this->b = $b;
    }

public function saySomething($something = 'something')
    {
    echo $something;
    }

/**
 * @return integer
 */
public function returnC()
    {
    return $this->a + $this->b;
    }

}

智能差分输出(MN 表示“M 行,N 列”):

C:>DMSSmartDifferencer PHP~PHP5 \temp\first_version.php \temp\second_version.php
Copyright (C) 2009-2012 Semantic Designs; All Rights Reserved
PHP~PHP5 SmartDifferencer Version 1.0.14
Copyright (C) 2012 Semantic Designs, Inc; All Rights Reserved; SD Confidential
Powered by DMS (R) Software Reengineering Toolkit
*** Unregistered SmartDifferencer Version 1.0
*** Operating with evaluation limits.

*** Parsing file C:/temp/first_version.php ...
*** Parsing file C:/temp/second_version.php ...
*** Creating suffix tree ...
*** Determining maximal pairs ...
*** Sorting maximal pairs ...
*** Determining differences ...
*** Printing edits ...
Substitute 7.13-7.14 by 7.13-7.18
<            $c
>            $a, $b
Substitute 10.9-10.21 by 11.9-12.22
<        $c = $a + $b;
>        $this->a = $a;
>        $this->b = $b;
At 15.12 insert 15.12-18.9 moving 19.21-19.32 to 15.21-15.32
>           function saySomething($something = 'something')
>        {
>        echo $something;
>        }
Delete 15.12-18.9 at 15.12 moving 15.21-15.27 to 23.21-23.27
<           function returnC()
<        {
<        return $this->c;
<        }
At 19.21 insert 23.21-23.27 moving 15.21-15.27 to 23.21-23.27
>                    returnC
Delete 19.21-19.32 at 23.21 moving 19.21-19.32 to 15.21-15.32
<                    saySomething
Substitute 21.9-21.25 by 25.9-25.35
<        echo 'something';
>        return $this->a + $this->b;
Exiting with final status 1.

您应该看到 SmartDifferencer 专注于代码结构中的增量,而不是中的增量。您注意到的第一件事是 SmartDifferencer 完全忽略了注释,因为它们对代码的作用没有影响。

但在下面的 unix diff 中,最明显的例子是多个 diff 将一个函数末尾的 delta 与另一个函数开头的 delta 混为一谈;没有程序员以这种方式解释代码中的差异。当试图理解真正发生了什么变化时,这种混乱的差异会使阅读变得混乱。

Unix风格差异的输出:

C:>diff \temp\first_version.php \temp\second_version.php
2c2
< // ----- first version -----
---
> // ----- second version -----
7c7,8
<     private $c;
---
>     private $a, $b;
>
10c11,17
<         $c = $a + $b;
---
>         $this->a = $a;
>         $this->b = $b;
>         }
>
>     public function saySomething($something = 'something')
>         {
>         echo $something;
11a19
>
13c21
<      * @return string
---
>      * @return integer
17,21c25
<         return $this->c;
<         }
<     public function saySomething()
<         {
<         echo 'something';
---
>         return $this->a + $this->b;
22a27
>

SmartDifferencer 和 diff 都不会告诉您接口的行为发生了变化,因为它所依赖的代码发生了变化。做到这一点的唯一方法是对直接或间接支持接口的所有代码进行静态语义分析,这是一个更难的问题。

于 2013-03-22T02:02:14.453 回答