Repository navigation
Expand file tree
/
Copy pathphp4-constructor-migrate.php
More file actions
73 lines (61 loc) · 2.04 KB
/
Copy pathphp4-constructor-migrate.php
File metadata and controls
73 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
use PhpParser\ParserFactory;
use PhpParser\PrettyPrinter;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Variable;
require __DIR__ . '/vendor/autoload.php';
ini_set('memory_limit', '-1');
$prettyPrinter = new PrettyPrinter\Standard;
$di = new RecursiveDirectoryIterator('.');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
if (strpos($filename, '.svn') === false && !is_dir($file)) {
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP5);
$stmts = "";
try {
$stmts = $parser->parse(file_get_contents($filename));
} catch (PhpParser\Error $e) {
echo 'Parse Error: ' . $e->getMessage() . "\n";
}
$matches = 0;
if (is_array($stmts)) {
$index = 0;
foreach ($stmts as $class) {
if($class instanceof PhpParser\Node\Stmt\Class_) {
$hasConstruct = false;
foreach ($class->stmts as $function) {
if($function instanceof PhpParser\Node\Stmt\ClassMethod) {
if ($function->name == '__construct') {
$hasConstruct = true;
}
}
}
if(!$hasConstruct) {
foreach ($class->stmts as $index => $function) {
if($function instanceof PhpParser\Node\Stmt\ClassMethod) {
if (strtolower($class->name) === strtolower($function->name)) {
$newFunction = new ClassMethod('__construct', array(), $function->getAttributes());
$newFunction->params = $function->params;
$args = array();
foreach ($function->params as $param) {
$args[] = $arg = new Arg(new Variable($param->name));
}
$newFunction->stmts[] = new StaticCall(new Name(array("self")), $function->name, $args);
array_splice($class->stmts, $index, 0, array($newFunction));
$matches++;
}
}
}
}
}
$index++;
}
}
if ($matches > 0) {
file_put_contents($filename, $prettyPrinter->prettyPrintFile($stmts));
echo "=== Updated " . $matches . " constructors in " . $filename . ".\n";
}
}
}