普通差异报告文本行方面的差异。软件是根据代码结构定义的。源代码作为文本行和源作为结构之间的不匹配使 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 都不会告诉您接口的行为发生了变化,因为它所依赖的代码发生了变化。做到这一点的唯一方法是对直接或间接支持接口的所有代码进行静态语义分析,这是一个更难的问题。