Skip to content

Commit

Permalink
Merge pull request #1118 from nunomaduro/analysis-0gVvYx
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI

[ci skip] [skip ci]
  • Loading branch information
nunomaduro committed Feb 9, 2022
2 parents e927f6a + c05edac commit e228362
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/ApplicationResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace NunoMaduro\Larastan;

use NunoMaduro\Larastan\Support\ClassMapGenerator;
use const DIRECTORY_SEPARATOR;
use Illuminate\Contracts\Foundation\Application;
use function in_array;
use NunoMaduro\Larastan\Support\ClassMapGenerator;
use Orchestra\Testbench\Concerns\CreatesApplication;

/**
Expand Down
105 changes: 55 additions & 50 deletions src/Support/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use Symfony\Component\Finder\Finder;

/**
* ClassMapGenerator
* ClassMapGenerator.
*
* @author Gyula Sallai <salla016@gmail.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
Expand All @@ -36,10 +36,10 @@ class ClassMapGenerator
private static $restPattern;

/**
* Generate a class map file
* Generate a class map file.
*
* @param \Traversable<string>|array<string> $dirs Directories or a single path to search in
* @param string $file The name of the class map file
* @param \Traversable<string>|array<string> $dirs Directories or a single path to search in
* @param string $file The name of the class map file
* @return void
*/
public static function dump($dirs, $file)
Expand All @@ -54,12 +54,13 @@ public static function dump($dirs, $file)
}

/**
* Checks if the given path is absolute
* Checks if the given path is absolute.
*
* @param string $path
* @param string $path
* @return bool
*
* @see \Composer\Util\Filesystem
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
Expand All @@ -72,10 +73,11 @@ public static function isAbsolutePath($path)
* Normalize a path. This replaces backslashes with slashes, removes ending
* slash and collapses redundant separators and up-level references.
*
* @param string $path Path to the file or directory
* @param string $path Path to the file or directory
* @return string
*
* @see \Composer\Util\Filesystem
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
Expand All @@ -101,26 +103,27 @@ public static function normalizePath($path)
foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && ($absolute || $up)) {
array_pop($parts);
$up = !(empty($parts) || '..' === end($parts));
$up = ! (empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) {
$parts[] = $chunk;
$up = '..' !== $chunk;
}
}

return $prefix . ($absolute ? '/' : '') . implode('/', $parts);
return $prefix.($absolute ? '/' : '').implode('/', $parts);
}

/**
* Iterate over all files in the given directory searching for classes
* Iterate over all files in the given directory searching for classes.
*
* @param \Traversable<\SplFileInfo>|string|array<string> $path The path to search in or an iterator
* @param string $excluded Regex that matches file paths to be excluded from the classmap
* @param ?\Composer\IO\IOInterface $io IO object
* @param ?string $namespace Optional namespace prefix to filter by
* @param ?string $autoloadType psr-0|psr-4 Optional autoload standard to use mapping rules
* @param array<string, true> $scannedFiles
* @param \Traversable<\SplFileInfo>|string|array<string> $path The path to search in or an iterator
* @param string $excluded Regex that matches file paths to be excluded from the classmap
* @param ?\Composer\IO\IOInterface $io IO object
* @param ?string $namespace Optional namespace prefix to filter by
* @param ?string $autoloadType psr-0|psr-4 Optional autoload standard to use mapping rules
* @param array<string, true> $scannedFiles
* @return array<class-string, string> A class map array
*
* @throws \RuntimeException When the path is neither an existing file nor directory
*/
public static function createMap($path, $excluded = null, $io = null, $namespace = null, $autoloadType = null, &$scannedFiles = [])
Expand All @@ -133,7 +136,7 @@ public static function createMap($path, $excluded = null, $io = null, $namespace
$path = Finder::create()->files()->followLinks()->name('/\.(php|inc|hh)$/')->in($path);
} else {
throw new \RuntimeException(
'Could not scan for classes inside "' . $path .
'Could not scan for classes inside "'.$path.
'" which does not appear to be a file nor a folder'
);
}
Expand All @@ -146,12 +149,12 @@ public static function createMap($path, $excluded = null, $io = null, $namespace

foreach ($path as $file) {
$filePath = $file->getPathname();
if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['php', 'inc', 'hh'])) {
if (! in_array(pathinfo($filePath, PATHINFO_EXTENSION), ['php', 'inc', 'hh'])) {
continue;
}

if (!self::isAbsolutePath($filePath)) {
$filePath = $cwd . '/' . $filePath;
if (! self::isAbsolutePath($filePath)) {
$filePath = $cwd.'/'.$filePath;
$filePath = self::normalizePath($filePath);
} else {
$filePath = Preg::replace('{[\\\\/]{2,}}', '/', $filePath);
Expand Down Expand Up @@ -193,12 +196,12 @@ public static function createMap($path, $excluded = null, $io = null, $namespace
continue;
}

if (!isset($map[$class])) {
if (! isset($map[$class])) {
$map[$class] = $filePath;
} elseif ($io && $map[$class] !== $filePath && !Preg::isMatch('{/(test|fixture|example|stub)s?/}i', strtr($map[$class] . ' ' . $filePath, '\\', '/'))) {
} elseif ($io && $map[$class] !== $filePath && ! Preg::isMatch('{/(test|fixture|example|stub)s?/}i', strtr($map[$class].' '.$filePath, '\\', '/'))) {
$io->writeError(
'<warning>Warning: Ambiguous class resolution, "' . $class . '"' .
' was found in both "' . $map[$class] . '" and "' . $filePath . '", the first will be used.</warning>'
'<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
);
}
}
Expand All @@ -208,14 +211,14 @@ public static function createMap($path, $excluded = null, $io = null, $namespace
}

/**
* Remove classes which could not have been loaded by namespace autoloaders
* Remove classes which could not have been loaded by namespace autoloaders.
*
* @param array<int, class-string> $classes found classes in given file
* @param string $filePath current file
* @param string $baseNamespace prefix of given autoload mapping
* @param string $namespaceType psr-0|psr-4
* @param string $basePath root directory of given autoload mapping
* @param ?\Composer\IO\IOInterface $io IO object
* @param array<int, class-string> $classes found classes in given file
* @param string $filePath current file
* @param string $baseNamespace prefix of given autoload mapping
* @param string $namespaceType psr-0|psr-4
* @param string $basePath root directory of given autoload mapping
* @param ?\Composer\IO\IOInterface $io IO object
* @return array<int, class-string> valid classes
*/
private static function filterByNamespace($classes, $filePath, $baseNamespace, $namespaceType, $basePath, $io)
Expand All @@ -239,7 +242,7 @@ private static function filterByNamespace($classes, $filePath, $baseNamespace, $
$namespace = substr($class, 0, $namespaceLength + 1);
$className = substr($class, $namespaceLength + 1);
$subPath = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
. str_replace('_', DIRECTORY_SEPARATOR, $className);
.str_replace('_', DIRECTORY_SEPARATOR, $className);
} else {
$subPath = str_replace('_', DIRECTORY_SEPARATOR, $class);
}
Expand All @@ -259,7 +262,7 @@ private static function filterByNamespace($classes, $filePath, $baseNamespace, $
if (empty($validClasses)) {
foreach ($rejectedClasses as $class) {
if ($io) {
$io->writeError("<warning>Class $class located in " . Preg::replace('{^' . preg_quote(getcwd()) . '}', '.', $filePath, 1) . " does not comply with $namespaceType autoloading standard. Skipping.</warning>");
$io->writeError("<warning>Class $class located in ".Preg::replace('{^'.preg_quote(getcwd()).'}', '.', $filePath, 1)." does not comply with $namespaceType autoloading standard. Skipping.</warning>");
}
}

Expand All @@ -270,11 +273,12 @@ private static function filterByNamespace($classes, $filePath, $baseNamespace, $
}

/**
* Extract the classes in the given file
* Extract the classes in the given file.
*
* @param string $path The file to check
* @throws \RuntimeException
* @param string $path The file to check
* @return array<int, class-string> The found classes
*
* @throws \RuntimeException
*/
private static function findClasses($path)
{
Expand All @@ -283,10 +287,10 @@ private static function findClasses($path)
// Use @ here instead of Silencer to actively suppress 'unhelpful' output
// @link https://github.com/composer/composer/pull/4886
$contents = @php_strip_whitespace($path);
if (!$contents) {
if (!file_exists($path)) {
if (! $contents) {
if (! file_exists($path)) {
$message = 'File at "%s" does not exist, check your classmap definitions';
} elseif (!is_readable($path)) {
} elseif (! is_readable($path)) {
$message = 'File at "%s" is not readable, check its permissions';
} elseif ('' === trim((string) file_get_contents($path))) {
// The input file was really empty and thus contains no classes
Expand All @@ -296,14 +300,14 @@ private static function findClasses($path)
}
$error = error_get_last();
if (isset($error['message'])) {
$message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
$message .= PHP_EOL.'The following message may be helpful:'.PHP_EOL.$error['message'];
}
throw new \RuntimeException(sprintf($message, $path));
}

// return early if there is no chance of matching anything in this file
Preg::matchAll('{\b(?:class|interface|trait' . $extraTypes . ')\s}i', $contents, $matches);
if (!$matches) {
Preg::matchAll('{\b(?:class|interface|trait'.$extraTypes.')\s}i', $contents, $matches);
if (! $matches) {
return [];
}

Expand Down Expand Up @@ -332,7 +336,7 @@ private static function findClasses($path)

Preg::matchAll('{
(?:
\b(?<![\$:>])(?P<type>class|interface|trait' . $extraTypes . ') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
\b(?<![\$:>])(?P<type>class|interface|trait'.$extraTypes.') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
| \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;]
)
}ix', $contents, $matches);
Expand All @@ -341,8 +345,8 @@ private static function findClasses($path)
$namespace = '';

for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
if (!empty($matches['ns'][$i])) {
$namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]) . '\\';
if (! empty($matches['ns'][$i])) {
$namespace = str_replace([' ', "\t", "\r", "\n"], '', $matches['nsname'][$i]).'\\';
} else {
$name = $matches['name'][$i];
// skip anon classes extending/implementing
Expand All @@ -351,7 +355,7 @@ private static function findClasses($path)
}
if ($name[0] === ':') {
// This is an XHP class, https://github.com/facebook/xhp
$name = 'xhp' . substr(str_replace(['-', ':'], ['_', '__'], $name), 1);
$name = 'xhp'.substr(str_replace(['-', ':'], ['_', '__'], $name), 1);
} elseif ($matches['type'][$i] === 'enum') {
// something like:
// enum Foo: int { HERP = '123'; }
Expand All @@ -366,7 +370,7 @@ private static function findClasses($path)
$name = substr($name, 0, $colonPos);
}
}
$classes[] = ltrim($namespace . $name, '\\');
$classes[] = ltrim($namespace.$name, '\\');
}
}

Expand All @@ -393,10 +397,11 @@ private static function getExtraTypes()
}

/**
* @param string[] $types
* @param string[] $types
* @return void
*
* @see \Composer\Autoload\PhpFileCleaner
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
Expand All @@ -406,10 +411,10 @@ private static function setTypeConfig($types)
self::$typeConfig[$type[0]] = [
'name' => $type,
'length' => strlen($type),
'pattern' => '{.\b(?<![\$:>])' . $type . '\s++[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+}Ais',
'pattern' => '{.\b(?<![\$:>])'.$type.'\s++[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+}Ais',
];
}

self::$restPattern = '{[^?"\'</' . implode('', array_keys(self::$typeConfig)) . ']+}A';
self::$restPattern = '{[^?"\'</'.implode('', array_keys(self::$typeConfig)).']+}A';
}
}

0 comments on commit e228362

Please sign in to comment.