Skip to content

Commit

Permalink
this commit is the result of finding a significant bottleneck in cach…
Browse files Browse the repository at this point in the history
…egrind files

and performance gain up to 4% is measured in testsuites (of local projects).

It fixes an inefficient loop in getHookMethods in which unnecessary logic can be executed
when method docblock are are not defined. It also minimizes calls to the same methods.

Also, the code is verbose enough to understand without the need for extra private methods.
  • Loading branch information
dynasource committed Nov 30, 2018
1 parent bb9029a commit 3c7aa92
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions src/Util/Test.php
Expand Up @@ -718,26 +718,30 @@ public static function getHookMethods(string $className): array
continue;
}

if (self::isBeforeClassMethod($method)) {
\array_unshift(
self::$hookMethods[$className]['beforeClass'],
$method->getName()
);
}

if (self::isBeforeMethod($method)) {
\array_unshift(
self::$hookMethods[$className]['before'],
$method->getName()
);
}
if ($methodComment = $method->getDocComment()) {
if ($method->isStatic()) {
if (\strpos($methodComment, '@beforeClass') !== false) {
\array_unshift(
self::$hookMethods[$className]['beforeClass'],
$method->getName()
);
}

if (\strpos($methodComment, '@afterClass') !== false) {
self::$hookMethods[$className]['afterClass'][] = $method->getName();
}
}

if (self::isAfterMethod($method)) {
self::$hookMethods[$className]['after'][] = $method->getName();
}
if (\preg_match('/@before\b/', $methodComment) > 0) {
\array_unshift(
self::$hookMethods[$className]['before'],
$method->getName()
);
}

if (self::isAfterClassMethod($method)) {
self::$hookMethods[$className]['afterClass'][] = $method->getName();
if (\preg_match('/@after\b/', $methodComment) > 0) {
self::$hookMethods[$className]['after'][] = $method->getName();
}
}
}
} catch (ReflectionException $e) {
Expand Down Expand Up @@ -1088,26 +1092,6 @@ private static function resolveReflectionObjectsToLines(array $reflectors): arra
return $result;
}

private static function isBeforeClassMethod(ReflectionMethod $method): bool
{
return $method->isStatic() && \strpos($method->getDocComment(), '@beforeClass') !== false;
}

private static function isBeforeMethod(ReflectionMethod $method): bool
{
return \preg_match('/@before\b/', $method->getDocComment()) > 0;
}

private static function isAfterClassMethod(ReflectionMethod $method): bool
{
return $method->isStatic() && \strpos($method->getDocComment(), '@afterClass') !== false;
}

private static function isAfterMethod(ReflectionMethod $method): bool
{
return \preg_match('/@after\b/', $method->getDocComment()) > 0;
}

/**
* Trims any extensions from version string that follows after
* the <major>.<minor>[.<patch>] format
Expand Down

0 comments on commit 3c7aa92

Please sign in to comment.