insphpect / staticanalysis
一系列静态分析工具
dev-master
2019-07-01 13:42 UTC
Requires
- php: >=7.0.0
Requires (Dev)
- phpunit/phpunit: ^5.7.20
This package is auto-updated.
Last update: 2024-08-29 05:00:46 UTC
README
这是我作为我的博士项目 Insphpect 的一部分开发的一些工具。
1. NamespaceResolve
这个工具可以用来解析任何文件中任何类的完整类名,包括命名空间。
file.php:
<?php namespace X; use Foo\Bar as Baz; $foo = new Baz;
解析器可以用来确定 new Baz
将会实例化 \Foo\Bar
。
$source = file_get_contents('file.php'); $resolver = new \Insphpect\StaticAnalysis\NamespaceResolve($source); $resolver->resolve('Baz'); // \Foo\Bar
查看测试用例以获取更多示例。
2. VariableResolve
这个工具试图解析 $code
中的任何 $line
上的 $variable
的内容。它不假设调用栈,并在需要解析参数时给出参数的编号。
尽可能提供值或代码块。如果值来自调用栈的较高层,则返回如 {ARG0}
或 {ARG2}
的值,指示函数调用时使用的参数索引。
示例 1
$code = '<?php class TestClass implements Foo { private $a; private $b; public function __construct($a, $b, $c) { $ff = 1; $this->a = $a; $this->b = new \B(str_replace($ff, trim(\'b\'), \'c\')); } }'; $resolver = new \Insphpect\StaticAnalysis\VariableResolve(); // Resolve the contents of the variable $ff on line 8 in $code $result = $resolver->resolve($code, '$ff', 8); // "1"
示例 2
$code = '<?php class TestClass implements Foo { private $a; private $b; public function __construct($a, $b, $c) { $this->a = $a; $this->b = new \B(str_replace($ff, trim(\'b\'), \'c\')); $ff = new \Something(); } }'; $resolver = new \Insphpect\StaticAnalysis\VariableResolve(); // Resolve the contents of $ff on line 10 $result = $resolver->resolve($code, '$ff', 10); // "new Something()"
示例 3
$code = '<?php class TestClass implements Foo { private $a; private $b; public function __construct($a, $b, $c) { $this->a = $a; $this->b = $b; } }'; $resolver = new \Insphpect\StaticAnalysis\VariableResolve(); // Resolve the contents of the variable $b on line 10 $result = $resolver->resolve($code, '$b', 10); // "{ARG1}"
示例 4
$code = '<?php class TestClass implements Foo { private $a; private $b; public function __construct($a, $b, $c) { $b = $b+1; $this->a = $a; $this->b = $b; } }'; $resolver = new \Insphpect\StaticAnalysis\VariableResolve(); // Resolve $b on line 11 of $code $result = $resolver->resolve($code, '$b', 11); // "{ARG1}+1"
查看测试用例以获取更多示例