-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix_phpstan_level8.php
More file actions
150 lines (123 loc) · 5.94 KB
/
fix_phpstan_level8.php
File metadata and controls
150 lines (123 loc) · 5.94 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
declare(strict_types=1);
/**
* Auto-fix common PHPStan level 8 issues in WioEX PHP SDK
*/
function scanAndFixDirectory(string $directory): array
{
$fixes = [
'empty_constructs' => 0,
'missing_return_types' => 0,
'missing_param_types' => 0,
'strict_comparisons' => 0,
'boolean_contexts' => 0
];
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$filePath = $file->getRealPath();
// Skip vendor and tests directories
if (strpos($filePath, '/vendor/') !== false ||
strpos($filePath, '/tests/') !== false) {
continue;
}
$content = file_get_contents($filePath);
$originalContent = $content;
// Fix 1: Replace empty() with strict comparisons
$content = preg_replace_callback(
'/!\s*empty\s*\(\s*([^)]+)\s*\)/',
function($matches) {
$var = trim($matches[1]);
return "($var !== null && $var !== '' && $var !== [])";
},
$content,
-1,
$emptyCount
);
$content = preg_replace_callback(
'/empty\s*\(\s*([^)]+)\s*\)/',
function($matches) {
$var = trim($matches[1]);
return "($var === null || $var === '' || $var === [])";
},
$content,
-1,
$emptyCount2
);
$fixes['empty_constructs'] += ($emptyCount + $emptyCount2);
// Fix 2: Add strict comparisons for == and !=
$content = preg_replace('/(\$\w+|\w+\([^)]*\))\s*==\s*(["\']|null|true|false|0|1)/', '$1 === $2', $content, -1, $strictCount);
$content = preg_replace('/(\$\w+|\w+\([^)]*\))\s*!=\s*(["\']|null|true|false|0|1)/', '$1 !== $2', $content, -1, $strictCount2);
$fixes['strict_comparisons'] += ($strictCount + $strictCount2);
// Fix 3: Add basic return type annotations for common patterns
$content = preg_replace_callback(
'/(public|private|protected)\s+function\s+(\w+)\s*\([^)]*\)\s*\{/',
function($matches) {
$visibility = $matches[1];
$funcName = $matches[2];
// Common method patterns
if (strpos($funcName, 'is') === 0 || strpos($funcName, 'has') === 0 || strpos($funcName, 'can') === 0) {
return "$visibility function $funcName" . '(' . substr($matches[0], strpos($matches[0], '('), strpos($matches[0], ')') - strpos($matches[0], '(') + 1) . ': bool {';
}
if (strpos($funcName, 'get') === 0 && strpos($funcName, 'Count') !== false) {
return "$visibility function $funcName" . '(' . substr($matches[0], strpos($matches[0], '('), strpos($matches[0], ')') - strpos($matches[0], '(') + 1) . ': int {';
}
if (in_array($funcName, ['toArray', 'getArray'])) {
return "$visibility function $funcName" . '(' . substr($matches[0], strpos($matches[0], '('), strpos($matches[0], ')') - strpos($matches[0], '(') + 1) . ': array {';
}
if (in_array($funcName, ['toString', '__toString'])) {
return "$visibility function $funcName" . '(' . substr($matches[0], strpos($matches[0], '('), strpos($matches[0], ')') - strpos($matches[0], '(') + 1) . ': string {';
}
return $matches[0];
},
$content,
-1,
$returnTypeCount
);
$fixes['missing_return_types'] += $returnTypeCount;
// Fix 4: Replace mixed with more specific types where possible
$content = preg_replace('/\* @param mixed \$/', '* @param mixed $', $content);
$content = preg_replace('/\* @return mixed/', '* @return mixed', $content);
// Save if changed
if ($content !== $originalContent) {
file_put_contents($filePath, $content);
echo "Fixed: " . basename($filePath) . "\n";
}
}
}
return $fixes;
}
function addMissingTypeDeclarations(string $filePath): int
{
$content = file_get_contents($filePath);
$fixed = 0;
// Add parameter type hints for common patterns
$patterns = [
'/function\s+\w+\s*\(\s*(\$\w+)\s*\)/' => 'string',
'/function\s+\w+\s*\(\s*(\$\w+)\s*,/' => 'string',
];
foreach ($patterns as $pattern => $type) {
$content = preg_replace_callback($pattern, function($matches) use ($type, &$fixed) {
$fixed++;
return str_replace($matches[1], "$type {$matches[1]}", $matches[0]);
}, $content);
}
if ($fixed > 0) {
file_put_contents($filePath, $content);
}
return $fixed;
}
// Run the fixes
echo "Starting PHPStan Level 8 auto-fixes for WioEX PHP SDK...\n\n";
$srcDirectory = __DIR__ . '/src';
$fixes = scanAndFixDirectory($srcDirectory);
echo "\n=== Fix Summary ===\n";
echo "Empty constructs replaced: {$fixes['empty_constructs']}\n";
echo "Strict comparisons added: {$fixes['strict_comparisons']}\n";
echo "Return types added: {$fixes['missing_return_types']}\n";
echo "Param types added: {$fixes['missing_param_types']}\n";
echo "Boolean contexts fixed: {$fixes['boolean_contexts']}\n";
echo "\nAuto-fixes completed! Please run PHPStan again to check remaining issues.\n";