Skip to content

Commit

Permalink
PHP8: Static Method Lint Test
Browse files Browse the repository at this point in the history
This commit adds a lint test to check the code for any static calls to a non-static function which throws a fatal error as of PHP 8.0.
  • Loading branch information
aydreeihn authored and JediKev committed Feb 7, 2022
1 parent 59dc587 commit 958a748
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions setup/test/tests/test.staticmethods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
require_once "class.test.php";

class StaticMethods extends Test {
var $name = "Methods that should be static";

static function ignore3rdparty() {
return false;
}

function testStaticMethods() {
$scripts = static::getAllScripts();
$static_functions = array();
foreach ($scripts as $s) {
$matches = array();
preg_match_all('/^\s*(?:\/\*[^*]*\*\/)?\s*'
.'(?:(?:private|public|protected|static|abstract)\s+)+'
.'function\s+&?\s*([^(\s]+)\s*\(/m',
file_get_contents($s), $matches);
$static_functions = array_merge($static_functions, $matches[1]);
}
foreach (static::find_function_calls($scripts) as $call) {
list($file, $no, $line, $func) = $call;
if ((strpos($line, 'parent::'.$func) === false) &&
(strpos($line, 'Unpacker::'.$func) === false) &&
(strpos($line, '::'.$func) !== false) && !in_array($func, $static_functions)) {
$this->fail($file, $no, "$func: Function should be static");
}
else {
$this->pass();
}
}
}
}

return 'StaticMethods';
?>

0 comments on commit 958a748

Please sign in to comment.