8000 Refactor inVendors to a private static method · symfony/symfony@e2cc16a · GitHub
[go: up one dir, main page]

Skip to content

Commit e2cc16a

Browse files
greg0ireGrégoire Paris
authored andcommitted
Refactor inVendors to a private static method
Now that we have php 7, we can afford to do this.
1 parent 2fb303b commit e2cc16a

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,6 @@ public static function register($mode = 0)
6161
return $memoizedMode = $mode;
6262
};
6363

64-
$inVendors = function ($path) {
65-
/** @var string[] absolute paths to vendor directories */
66-
static $vendors;
67-
if (null === $vendors) {
68-
foreach (get_declared_classes() as $class) {
69-
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
70-
$r = new \ReflectionClass($class);
71-
$v = dirname(dirname($r->getFileName()));
72-
if (file_exists($v.'/composer/installed.json')) {
73-
$vendors[] = $v;
74-
}
75-
}
76-
}
77-
}
78-
$path = realpath($path) ?: $path;
79-
foreach ($vendors as $vendor) {
80-
if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
81-
return true;
82-
}
83-
}
84-
85-
return false;
86-
};
8764

8865
$deprecations = array(
8966
'unsilencedCount' => 0,
@@ -97,7 +74,7 @@ public static function register($mode = 0)
9774
'other' => array(),
9875
'remaining vendor' => array(),
9976
);
100-
$deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode, $UtilPrefix, $inVendors) {
77+
$deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode, $UtilPrefix) {
10178
$mode = $getMode();
10279
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode) {
10380
$ErrorHandler = $UtilPrefix.'ErrorHandler';
@@ -108,7 +85,7 @@ public static function register($mode = 0)
10885
$trace = debug_backtrace(true);
10986
$group = 'other';
11087

111-
$isWeak = DeprecationErrorHandler::MODE_WEAK === $mode || (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && $isVendor = $inVendors($file));
88+
$isWeak = DeprecationErrorHandler::MODE_WEAK === $mode || (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && $isVendor = self::inVendors($file));
11289

11390
$i = count($trace);
11491
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
@@ -246,6 +223,31 @@ public static function register($mode = 0)
246223
}
247224
}
248225

226+
private static function inVendors(string $path): bool
227+
{
228+
/** @var string[] absolute paths to vendor directories */
229+
static $vendors;
230+
if (null === $vendors) {
231+
foreach (get_declared_classes() as $class) {
232+
if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
233+
$r = new \ReflectionClass($class);
234+
$v = dirname(dirname($r->getFileName()));
235+
if (file_exists($v.'/composer/installed.json')) {
236+
$vendors[] = $v;
237+
}
238+
}
239+
}
240+
}
241+
$path = realpath($path) ?: $path;
242+
foreach ($vendors as $vendor) {
243+
if (0 === strpos($path, $vendor) && false !== strpbrk(substr($path, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
244+
return true;
245+
}
246+
}
247+
248+
return false;
249+
}
250+
249251
public static function collectDeprecations($outputFile)
250252
{
251253
$deprecations = array();

0 commit comments

Comments
 (0)
0