Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

$header = <<<EOF
This file is part of Composer.

(c) Nils Adermann <naderman@naderman.de>
Jordi Boggiano <j.boggiano@seld.be>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

$finder = PhpCsFixer\Finder::create()
->files()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->name('*.php')
->notPath('Fixtures')
;

$config = new PhpCsFixer\Config();
return $config
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setRules([
'@PSR2' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => ['statements' => ['declare', 'return']],
'cast_spaces' => ['space' => 'single'],
'header_comment' => ['header' => $header],
'statement_indentation' => ['stick_comment_to_next_continuous_control_statement' => true],
'include' => true,

'class_attributes_separation' => ['elements' => ['method' => 'one', 'trait_import' => 'none']],
'no_blank_lines_after_class_opening' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_statement' => true,
'no_extra_blank_lines' => true,
'no_leading_namespace_whitespace' => true,
'no_trailing_comma_in_singleline' => true,
'no_whitespace_in_blank_line' => true,
'object_operator_without_whitespace' => true,
//'phpdoc_align' => true,
'phpdoc_indent' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'phpdoc_no_access' => true,
'phpdoc_no_package' => true,
//'phpdoc_order' => true,
'phpdoc_scalar' => true,
'phpdoc_trim' => true,
'phpdoc_types' => true,
'psr_autoloading' => true,
'blank_lines_before_namespace' => true,
'standardize_not_equals' => true,
'ternary_operator_spaces' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'unary_operator_spaces' => true,

'native_function_invocation' => [
'include' => ['@compiler_optimized'], // Targets functions with special Zend opcodes (e.g., strlen, count)
'scope' => 'namespaced', // Only fixes functions inside a namespace
'strict' => true, // Removes leading \ if not native
],

// imports
'no_unused_imports' => true,
'fully_qualified_strict_types' => true,
'single_line_after_imports' => true,
//'global_namespace_import' => ['import_classes' => true],
'no_leading_import_slash' => true,
'single_import_per_statement' => true,

// PHP 7.2 migration
'array_syntax' => true,
'list_syntax' => true,
'regular_callable_call' => true,
'static_lambda' => true,
'nullable_type_declaration_for_default_null_value' => true,
'explicit_indirect_variable' => true,
'visibility_required' => ['elements' => ['property', 'method', 'const']],
'non_printable_character' => true,
'combine_nested_dirname' => true,
'random_api_migration' => true,
'ternary_to_null_coalescing' => true,
'phpdoc_to_param_type' => false,
'declare_strict_types' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => true,
],

// TODO php 7.4 migration (one day..)
// 'phpdoc_to_property_type' => true,
])
->setUsingCache(true)
->setRiskyAllowed(true)
->setFinder($finder)
;
4 changes: 2 additions & 2 deletions src/ClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getAmbiguousClasses($duplicatesFilter = '{/(test|fixture|example

$ambiguousClasses = [];
foreach ($this->ambiguousClasses as $class => $paths) {
$paths = array_filter($paths, function ($path) use ($duplicatesFilter): bool {
$paths = array_filter($paths, static function ($path) use ($duplicatesFilter): bool {
return !Preg::isMatch($duplicatesFilter, strtr($path, '\\', '/'));
});
if (\count($paths) > 0) {
Expand Down Expand Up @@ -157,7 +157,7 @@ public function clearPsrViolationsByPath(string $pathPrefix): void
$pathPrefix = rtrim(strtr($pathPrefix, '\\', '/'), '/');

foreach ($this->psrViolations as $path => $violations) {
if ($path === $pathPrefix || 0 === \strpos($path, $pathPrefix.'/')) {
if ($path === $pathPrefix || 0 === strpos($path, $pathPrefix.'/')) {
unset($this->psrViolations[$path]);
}
}
Expand Down
23 changes: 9 additions & 14 deletions src/ClassMapGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

use Composer\Pcre\Preg;
use Symfony\Component\Finder\Finder;
use Composer\IO\IOInterface;

/**
* ClassMapGenerator
Expand Down Expand Up @@ -57,7 +56,7 @@ public function __construct(array $extensions = ['php', 'inc'])
{
$this->extensions = $extensions;
$this->classMap = new ClassMap;
$this->streamWrappersRegex = sprintf('{^(?:%s)://}', implode('|', array_map('preg_quote', stream_get_wrappers())));
$this->streamWrappersRegex = \sprintf('{^(?:%s)://}', implode('|', array_map('preg_quote', stream_get_wrappers())));
}

/**
Expand Down Expand Up @@ -109,21 +108,21 @@ public function getClassMap(): ClassMap
*/
public function scanPaths($path, ?string $excluded = null, string $autoloadType = 'classmap', ?string $namespace = null, array $excludedDirs = []): void
{
if (!in_array($autoloadType, ['psr-0', 'psr-4', 'classmap'], true)) {
if (!\in_array($autoloadType, ['psr-0', 'psr-4', 'classmap'], true)) {
throw new \InvalidArgumentException('$autoloadType must be one of: "psr-0", "psr-4" or "classmap"');
}

if ('classmap' !== $autoloadType) {
if (!is_string($path)) {
if (!\is_string($path)) {
throw new \InvalidArgumentException('$path must be a string when specifying a psr-0 or psr-4 autoload type');
}
if (!is_string($namespace)) {
if (!\is_string($namespace)) {
throw new \InvalidArgumentException('$namespace must be given (even if it is an empty string if you do not want to filter) when specifying a psr-0 or psr-4 autoload type');
}
$basePath = $path;
}

if (is_string($path)) {
if (\is_string($path)) {
if (is_file($path)) {
$path = [new \SplFileInfo($path)];
} elseif (is_dir($path) || strpos($path, '*') !== false) {
Expand All @@ -144,7 +143,7 @@ public function scanPaths($path, ?string $excluded = null, string $autoloadType

foreach ($path as $file) {
$filePath = $file->getPathname();
if (!in_array(pathinfo($filePath, PATHINFO_EXTENSION), $this->extensions, true)) {
if (!\in_array(pathinfo($filePath, PATHINFO_EXTENSION), $this->extensions, true)) {
continue;
}

Expand Down Expand Up @@ -224,7 +223,7 @@ private function filterByNamespace(array $classes, string $filePath, string $bas
$validClasses = [];
$rejectedClasses = [];

$realSubPath = substr($filePath, strlen($basePath) + 1);
$realSubPath = substr($filePath, \strlen($basePath) + 1);
$dotPosition = strrpos($realSubPath, '.');
$realSubPath = substr($realSubPath, 0, $dotPosition === false ? PHP_INT_MAX : $dotPosition);

Expand All @@ -246,7 +245,7 @@ private function filterByNamespace(array $classes, string $filePath, string $bas
$subPath = str_replace('_', DIRECTORY_SEPARATOR, $class);
}
} elseif ('psr-4' === $namespaceType) {
$subNamespace = ('' !== $baseNamespace) ? substr($class, strlen($baseNamespace)) : $class;
$subNamespace = ('' !== $baseNamespace) ? substr($class, \strlen($baseNamespace)) : $class;
$subPath = str_replace('\\', DIRECTORY_SEPARATOR, $subNamespace);
} else {
throw new \InvalidArgumentException('$namespaceType must be "psr-0" or "psr-4"');
Expand Down Expand Up @@ -281,9 +280,6 @@ private function filterByNamespace(array $classes, string $filePath, string $bas
* Checks if the given path is absolute
*
* @see Composer\Util\Filesystem::isAbsolutePath
*
* @param string $path
* @return bool
*/
private static function isAbsolutePath(string $path): bool
{
Expand All @@ -297,7 +293,6 @@ private static function isAbsolutePath(string $path): bool
* @see Composer\Util\Filesystem::normalizePath
*
* @param string $path Path to the file or directory
* @return string
*/
private static function normalizePath(string $path): string
{
Expand Down Expand Up @@ -335,7 +330,7 @@ private static function normalizePath(string $path): string
}

// ensure c: is normalized to C:
$prefix = Preg::replaceCallback('{(?:^|://)[a-z]:$}i', function (array $m) { return strtoupper((string) $m[0]); }, $prefix);
$prefix = Preg::replaceCallback('{(?:^|://)[a-z]:$}i', static function (array $m) { return strtoupper((string) $m[0]); }, $prefix);

return $prefix.$absolute.implode('/', $parts);
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhpFileCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function clean(): string
if ($this->maxMatches === 1 && isset(self::$typeConfig[$char])) {
$type = self::$typeConfig[$char];
if (
\substr($this->contents, $this->index, $type['length']) === $type['name']
substr($this->contents, $this->index, $type['length']) === $type['name']
&& Preg::isMatch($type['pattern'], $this->contents, $match, 0, $this->index - 1)
) {
return $clean . $match[0];
Expand All @@ -130,7 +130,7 @@ public function clean(): string
$this->index += 1;
$skip = strcspn($this->contents, self::$rejectChars, $this->index);
if ($skip > 0) {
$clean .= $char . \substr($this->contents, $this->index, $skip);
$clean .= $char . substr($this->contents, $this->index, $skip);
$this->index += $skip;
} else {
$clean .= $char;
Expand Down Expand Up @@ -212,7 +212,7 @@ private function skipHeredoc(string $delimiter): void
continue 2;
case $firstDelimiterChar:
if (
\substr($this->contents, $this->index, $delimiterLength) === $delimiter
substr($this->contents, $this->index, $delimiterLength) === $delimiter
&& $this->match($delimiterPattern)
) {
$this->index += $delimiterLength;
Expand Down
12 changes: 6 additions & 6 deletions src/PhpFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static function findClasses(string $path): array
{
$extraTypes = self::getExtraTypes();

if (!function_exists('php_strip_whitespace')) {
if (!\function_exists('php_strip_whitespace')) {
throw new RuntimeException('Classmap generation relies on the php_strip_whitespace function, but it has been disabled by the disable_functions directive.');
}

Expand All @@ -55,7 +55,7 @@ public static function findClasses(string $path): array
$message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
}

throw new RuntimeException(sprintf($message, $path));
throw new RuntimeException(\sprintf($message, $path));
}

// return early if there is no chance of matching anything in this file
Expand All @@ -64,7 +64,7 @@ public static function findClasses(string $path): array
return [];
}

$p = new PhpFileCleaner($contents, count($matches[0]));
$p = new PhpFileCleaner($contents, \count($matches[0]));
$contents = $p->clean();
unset($p);

Expand All @@ -78,12 +78,12 @@ public static function findClasses(string $path): array
$classes = [];
$namespace = '';

for ($i = 0, $len = count($matches['type']); $i < $len; ++$i) {
for ($i = 0, $len = \count($matches['type']); $i < $len; ++$i) {
if (isset($matches['ns'][$i]) && $matches['ns'][$i] !== '') {
$namespace = str_replace([' ', "\t", "\r", "\n"], '', (string) $matches['nsname'][$i]) . '\\';
} else {
$name = $matches['name'][$i];
assert(is_string($name));
\assert(\is_string($name));
// skip anon classes extending/implementing
if ($name === 'extends') {
continue;
Expand Down Expand Up @@ -126,7 +126,7 @@ private static function getExtraTypes(): string
if (null === $extraTypes) {
$extraTypes = '';
$extraTypesArray = [];
if (PHP_VERSION_ID >= 80100 || (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>='))) {
if (PHP_VERSION_ID >= 80100 || (\defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>='))) {
$extraTypes .= '|enum';
$extraTypesArray = ['enum'];
}
Expand Down
Loading