Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix inefficient loop in getHookMethods and save up to 4% in cachegrind files #3429

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 22 additions & 38 deletions src/Util/Test.php
Expand Up @@ -715,26 +715,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 @@ -1085,26 +1089,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