|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Automattic\Jetpack\Analyzer; |
|
4
|
|
|
|
|
5
|
|
|
use PhpParser\ParserFactory; |
|
6
|
|
|
use PhpParser\NodeTraverser; |
|
7
|
|
|
use PhpParser\NodeDumper; |
|
8
|
|
|
use PhpParser\NodeVisitor\NameResolver; |
|
9
|
|
|
|
|
10
|
|
|
class Differences extends PersistentList { |
|
11
|
|
|
|
|
12
|
|
|
public function find( $new_declarations, $prev_declarations ) { |
|
13
|
|
|
$total = 0; |
|
14
|
|
|
// for each declaration, see if it exists in the current analyzer's declarations |
|
15
|
|
|
// if not, add it to the list of differences - either as missing or different |
|
16
|
|
|
foreach( $prev_declarations->get() as $prev_declaration ) { |
|
17
|
|
|
$matched = false; |
|
18
|
|
|
foreach( $new_declarations->get() as $new_declaration ) { |
|
19
|
|
|
if ( $prev_declaration->match( $new_declaration ) ) { |
|
20
|
|
|
$matched = true; |
|
21
|
|
|
break; |
|
22
|
|
|
} |
|
23
|
|
|
} |
|
24
|
|
|
if ( ! $matched ) { |
|
25
|
|
|
switch( $prev_declaration->type() ) { |
|
26
|
|
|
case 'class': |
|
27
|
|
|
$this->add( new Differences\Class_Missing( $prev_declaration ) ); |
|
28
|
|
|
break; |
|
29
|
|
|
case 'method': |
|
30
|
|
|
$this->add( new Differences\Class_Method_Missing( $prev_declaration ) ); |
|
31
|
|
|
break; |
|
32
|
|
|
default: |
|
33
|
|
|
echo "Unknown unmatched type " . $prev_declaration->type() . "\n"; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
$total += 1; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
echo "Total: $total\n"; |
|
40
|
|
|
echo "Missing: " . count( $this->get() ) . "\n"; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Scans the file for any invocations that depend on missing or different classes, methods, properties and functions |
|
45
|
|
|
*/ |
|
46
|
|
|
public function check_file_compatibility( $file_path ) { |
|
47
|
|
|
$source = file_get_contents( $file_path ); |
|
48
|
|
|
$parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 ); |
|
49
|
|
|
try { |
|
50
|
|
|
$ast = $parser->parse( $source ); |
|
51
|
|
|
} catch ( Error $error ) { |
|
|
|
|
|
|
52
|
|
|
echo "Parse error: {$error->getMessage()}\n"; |
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// $dumper = new NodeDumper; |
|
57
|
|
|
// echo $dumper->dump($ast) . "\n"; |
|
58
|
|
|
|
|
59
|
|
|
// before parsing, make sure we try to resolve class names |
|
60
|
|
|
$traverser = new NodeTraverser(); |
|
61
|
|
|
$nameResolver = new NameResolver(); |
|
62
|
|
|
$traverser->addVisitor( $nameResolver ); |
|
63
|
|
|
|
|
64
|
|
|
// Resolve names |
|
65
|
|
|
$ast = $traverser->traverse( $ast ); |
|
66
|
|
|
|
|
67
|
|
|
$traverser = new NodeTraverser(); |
|
68
|
|
|
$invocations = new Invocations(); |
|
69
|
|
|
$invocation_finder = new Invocations\Visitor( $file_path, $invocations ); |
|
70
|
|
|
$traverser->addVisitor( $invocation_finder ); |
|
71
|
|
|
$ast = $traverser->traverse( $ast ); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
print_r($invocations); |
|
74
|
|
|
$invocations->print(); |
|
75
|
|
|
// return $invocations; |
|
76
|
|
|
|
|
77
|
|
|
// $dumper = new NodeDumper; |
|
78
|
|
|
// echo $dumper->dump($ast) . "\n"; |
|
79
|
|
|
|
|
80
|
|
|
// TODO: return a list of warnings and errors |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Scan every invocation to see if it depends on a Difference |
|
84
|
|
|
*/ |
|
85
|
|
|
$warnings = new Warnings(); |
|
86
|
|
|
foreach( $invocations->get() as $invocation ) { |
|
87
|
|
|
foreach( $this->get() as $difference ) { |
|
88
|
|
|
// $warning = $ |
|
89
|
|
|
$difference->find_invocation_warnings( $invocation, $warnings ); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $warnings; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
Scrutinizer analyzes your
composer.json/composer.lockfile if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.