Created
July 24, 2020 18:09
-
-
Save iggyvolz/3d4dec55f9cd34703682e1d4f145a70c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getAllParents(ReflectionClass $refl):array | |
{ | |
return array_unique(iterator_to_array(getAllParentsIt($refl))); | |
} | |
function getAllParentsIt(ReflectionClass $refl):Iterator | |
{ | |
if($parent = $refl->getParentClass()) { | |
yield $parent; | |
yield from getAllParentsIt($parent); | |
} | |
foreach($refl->getInterfaces() as $interface) { | |
yield $interface; | |
yield from getAllParentsIt($interface); | |
} | |
foreach($refl->getTraits() as $trait) { | |
yield $trait; | |
yield from getAllParentsIt($trait); | |
} | |
} | |
foreach(get_declared_classes() as $class) { | |
$refl = new ReflectionClass($class); | |
foreach($refl->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED) as $method) { | |
$methodName = $method->name; | |
foreach($method->getParameters() as $paramPosition => $param) { | |
$childName = $param->name; | |
foreach(getAllParents($refl) as $parent) { | |
$parentClass = $parent->name; | |
// Skip internal classes with inconsistent names | |
if($parentClass === ArrayAccess::class) continue; | |
if($parentClass === SeekableIterator::class) continue; | |
if($parentClass === SplFileInfo::class) continue; | |
if($parent->hasMethod($method->name)) { | |
$parentMethod = $parent->getMethod($methodName); | |
if($parentMethod->isPrivate()) { | |
// Private method will not be called even though it has the same name, skip | |
continue; | |
} | |
if($parentMethod->getNumberOfParameters() <= $paramPosition) { | |
// The parameter in the child does not exist in the parent, skip | |
continue; | |
} | |
// Assert that the parameter of parent at position $paramPosition has the same name $canonicalName | |
$parentParameter = $parentMethod->getParameters()[$paramPosition]; | |
$parentName = $parentParameter->name; | |
if($parentName !== $childName) | |
{ | |
echo "Parameter $childName at position $paramPosition of $class:$methodName does not match parameter $parentName at position $paramPosition of $parentClass:$methodName".PHP_EOL; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment