diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index c46f37fb..5b26aded 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,261 +1,17 @@ parameters: ignoreErrors: - - - message: "#^Only booleans are allowed in &&, mixed given on the left side\\.$#" - count: 1 - path: src/Builder/ArchiveBuilder.php - - - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 4 - path: src/Builder/PackagesBuilder.php - - - - message: "#^Dynamic call to static method Composer\\\\Json\\\\JsonFile\\:\\:encode\\(\\)\\.$#" - count: 1 - path: src/Builder/PackagesBuilder.php - - - - message: "#^Only booleans are allowed in an if condition, int\\ given\\.$#" - count: 1 - path: src/Builder/PackagesBuilder.php - - - - message: "#^Only booleans are allowed in an if condition, string\\|null given\\.$#" - count: 1 - path: src/Builder/PackagesBuilder.php - - - - message: "#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#" - count: 1 - path: src/Builder/WebBuilder.php - - - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 2 - path: src/Console/Command/AddCommand.php - - - - message: "#^Only booleans are allowed in a negated boolean, Composer\\\\Repository\\\\Vcs\\\\VcsDriverInterface\\|null given\\.$#" - count: 1 - path: src/Console/Command/AddCommand.php - - - - message: "#^Only booleans are allowed in a negated boolean, mixed given\\.$#" - count: 2 - path: src/Console/Command/BuildCommand.php - - - - message: "#^Only booleans are allowed in \\|\\|, mixed given on the right side\\.$#" - count: 1 - path: src/Console/Command/BuildCommand.php - - - - message: "#^Only booleans are allowed in &&, string\\|null given on the left side\\.$#" - count: 1 - path: src/Console/Command/InitCommand.php - - - - message: "#^Strict comparison using \\!\\=\\= between '' and non\\-falsy\\-string will always evaluate to true\\.$#" - count: 1 - path: src/Console/Command/InitCommand.php - - - - message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#" - count: 1 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 1 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Foreach overwrites \\$file with its value variable\\.$#" - count: 2 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Only booleans are allowed in a negated boolean, int given\\.$#" - count: 1 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#" - count: 1 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#" - count: 1 - path: src/Console/Command/PurgeCommand.php - - - - message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#" - count: 5 - path: src/PackageSelection/PackageSelection.php - - message: "#^Call to function is_string\\(\\) with string will always evaluate to true\\.$#" count: 1 path: src/PackageSelection/PackageSelection.php - - - message: "#^Casting to bool something that's already bool\\.$#" - count: 1 - path: src/PackageSelection/PackageSelection.php - - - - message: "#^Only booleans are allowed in &&, string given on the left side\\.$#" - count: 1 - path: src/PackageSelection/PackageSelection.php - - - - message: "#^Only booleans are allowed in a negated boolean, int\\<0, max\\> given\\.$#" - count: 1 - path: src/PackageSelection/PackageSelection.php - - - - message: "#^Only booleans are allowed in a ternary operator condition, string given\\.$#" - count: 1 - path: src/PackageSelection/PackageSelection.php - - - - message: "#^Only booleans are allowed in an if condition, string\\|null given\\.$#" - count: 2 - path: src/PackageSelection/PackageSelection.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 2 - path: tests/Builder/ArchiveBuilderHelperTest.php - - message: "#^AnonymousClassdd4f4d5d0a10081f3f8b9c19006facf2\\:\\:__construct\\(\\) does not call parent constructor from Composer\\\\Package\\\\Archiver\\\\ArchiveManager\\.$#" count: 1 path: tests/Builder/ArchiveBuilderTest.php - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\)\\.$#" - count: 2 - path: tests/Builder/ArchiveBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\TestCase\\:\\:returnCallback\\(\\)\\.$#" - count: 1 - path: tests/Builder/ArchiveBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\TestCase\\:\\:returnValue\\(\\)\\.$#" - count: 1 - path: tests/Builder/ArchiveBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayHasKey\\(\\)\\.$#" - count: 1 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertArrayNotHasKey\\(\\)\\.$#" - count: 1 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 3 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertFalse\\(\\)\\.$#" - count: 1 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\)\\.$#" - count: 2 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Only booleans are allowed in a negated boolean, string\\|null given\\.$#" - count: 1 - path: tests/Builder/PackagesBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertFalse\\(\\)\\.$#" - count: 1 - path: tests/Builder/WebBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertMatchesRegularExpression\\(\\)\\.$#" - count: 6 - path: tests/Builder/WebBuilderDumpTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 3 - path: tests/Builder/WebBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertInstanceOf\\(\\)\\.$#" - count: 1 - path: tests/Builder/WebBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\)\\.$#" - count: 3 - path: tests/Builder/WebBuilderTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEmpty\\(\\)\\.$#" - count: 3 - path: tests/PackageSelection/PackageSelectionLoadTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertNotEmpty\\(\\)\\.$#" - count: 3 - path: tests/PackageSelection/PackageSelectionLoadTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertNotInstanceOf\\(\\)\\.$#" - count: 1 - path: tests/PackageSelection/PackageSelectionLoadTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\)\\.$#" - count: 2 - path: tests/PackageSelection/PackageSelectionLoadTest.php - - - - message: "#^Construct empty\\(\\) is not allowed\\. Use more strict comparison\\.$#" - count: 1 - path: tests/PackageSelection/PackageSelectionTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#" - count: 6 - path: tests/PackageSelection/PackageSelectionTest.php - - - - message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertSame\\(\\)\\.$#" - count: 2 - path: tests/PackageSelection/PackageSelectionTest.php - - - - message: "#^Implicit array creation is not allowed \\- variable \\$data does not exist\\.$#" - count: 1 - path: tests/PackageSelection/PackageSelectionTest.php - - - - message: "#^Implicit array creation is not allowed \\- variable \\$repo does not exist\\.$#" - count: 1 - path: tests/PackageSelection/PackageSelectionTest.php - - message: "#^Offset 'url' on array\\{package\\: array\\} on left side of \\?\\? does not exist\\.$#" count: 1 path: tests/PackageSelection/PackageSelectionTest.php - - - message: "#^Variable \\$filterPackages on left side of \\?\\? always exists and is always null\\.$#" - count: 1 - path: tests/PackageSelection/PackageSelectionTest.php diff --git a/src/Builder/ArchiveBuilder.php b/src/Builder/ArchiveBuilder.php index 7be5f160..6fc4b28f 100644 --- a/src/Builder/ArchiveBuilder.php +++ b/src/Builder/ArchiveBuilder.php @@ -55,7 +55,7 @@ public function dump(array $packages): void $progressBar = null; $hasStarted = false; $verbosity = $this->output->getVerbosity(); - $renderProgress = $this->input->getOption('stats') && OutputInterface::VERBOSITY_NORMAL == $verbosity; + $renderProgress = (bool) $this->input->getOption('stats') && OutputInterface::VERBOSITY_NORMAL == $verbosity; if ($renderProgress) { $packageCount = 0; diff --git a/src/Builder/PackagesBuilder.php b/src/Builder/PackagesBuilder.php index bce34f0a..89c35f01 100644 --- a/src/Builder/PackagesBuilder.php +++ b/src/Builder/PackagesBuilder.php @@ -24,14 +24,13 @@ class PackagesBuilder extends Builder { public const MINIFY_ALGORITHM_V2 = 'composer/2.0'; - /** @var string packages.json file name. */ - private $filename; - /** @var string included json filename template */ - private $includeFileName; + /** packages.json file name. */ + private string $filename; + /** included json filename template */ + private string $includeFileName; /** @var list */ - private $writtenIncludeJsons = []; - /** @var bool */ - private $minify; + private array $writtenIncludeJsons = []; + private bool $minify; /** * @param array $config @@ -61,7 +60,7 @@ public function dump(array $packages): void $repo = ['packages' => []]; if (isset($this->config['providers']) && true === $this->config['providers']) { $providersUrl = 'p/%package%$%hash%.json'; - if (!empty($this->config['homepage'])) { + if (isset($this->config['homepage']) && is_string($this->config['homepage'])) { $repo['providers-url'] = parse_url(rtrim($this->config['homepage'], '/'), PHP_URL_PATH) . '/' . $providersUrl; } else { $repo['providers-url'] = $providersUrl; @@ -93,13 +92,13 @@ public function dump(array $packages): void // Composer 2.0 format $metadataUrl = 'p2/%package%.json'; - if (!empty($this->config['homepage'])) { + if (array_key_exists('homepage', $this->config) && false !== filter_var($this->config['homepage'], FILTER_VALIDATE_URL)) { $repo['metadata-url'] = parse_url(rtrim($this->config['homepage'], '/'), PHP_URL_PATH) . '/' . $metadataUrl; } else { $repo['metadata-url'] = $metadataUrl; } - if (!empty($this->config['available-package-patterns'])) { + if (array_key_exists('available-package-patterns', $this->config) && count($this->config['available-package-patterns']) > 0) { $repo['available-package-patterns'] = $this->config['available-package-patterns']; } else { $repo['available-packages'] = array_keys($packagesByName); @@ -154,7 +153,7 @@ private function findReplacements(array $packages, string $replaced): array $replacements = []; foreach ($packages as $packageName => $packageConfig) { foreach ($packageConfig as $versionConfig) { - if (!empty($versionConfig['replace']) && array_key_exists($replaced, $versionConfig['replace'])) { + if (array_key_exists('replace', $versionConfig) && array_key_exists($replaced, $versionConfig['replace'])) { $replacements[$packageName] = $packageConfig; break; } @@ -246,7 +245,7 @@ private function dumpPackageIncludeJson(array $packages, string $includesUrl, st $options |= JSON_PRETTY_PRINT; } - $contents = $repoJson->encode(array_merge(['packages' => $packages], $additionalMetaData), $options) . "\n"; + $contents = $repoJson::encode(array_merge(['packages' => $packages], $additionalMetaData), $options) . "\n"; $hash = hash($hashAlgorithm, $contents); if (false !== strpos($includesUrl, '%hash%')) { @@ -259,7 +258,7 @@ private function dumpPackageIncludeJson(array $packages, string $includesUrl, st } } - if ($path) { + if (is_string($path)) { $this->writeToFile($path, $contents); $this->output->writeln("Wrote packages to $path"); } @@ -296,7 +295,7 @@ private function writeToFile(string $path, string $contents): void file_put_contents($path, $contents); break; } catch (\Exception $e) { - if ($retries) { + if ($retries > 0) { usleep(500000); continue; } diff --git a/src/Builder/WebBuilder.php b/src/Builder/WebBuilder.php index ca36af08..a7fe7da7 100644 --- a/src/Builder/WebBuilder.php +++ b/src/Builder/WebBuilder.php @@ -52,7 +52,7 @@ public function dump(array $packages): void $this->output->writeln('Define a "name" property in your json config to name the repository'); } - if (!$this->rootPackage->getHomepage()) { + if (is_null($this->rootPackage->getHomepage())) { $this->output->writeln('Define a "homepage" property in your json config to configure the repository URL'); } diff --git a/src/Console/Command/AddCommand.php b/src/Console/Command/AddCommand.php index 0225df9c..c94f26d0 100644 --- a/src/Console/Command/AddCommand.php +++ b/src/Console/Command/AddCommand.php @@ -97,7 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $repositoryConfig = ['type' => $vcsDriver, 'url' => $repositoryUrl]; - if (!empty($repositoryName)) { + if (is_string($repositoryName) && '' !== $repositoryName) { $repositoryConfig['name'] = $repositoryName; } @@ -122,12 +122,13 @@ protected function isRepositoryValid(string $repositoryUrl, string $type): bool $downloader = new HttpDownloader($io, $config); $repository = new VcsRepository(['url' => $repositoryUrl, 'type' => $type], $io, $config, $downloader); - if (!($driver = $repository->getDriver())) { + $driver = $repository->getDriver(); + if (is_null($driver)) { return false; } $information = $driver->getComposerInformation($driver->getRootIdentifier()); - return !empty($information['name']); + return isset($information['name']) && is_string($information['name']); } } diff --git a/src/Console/Command/BuildCommand.php b/src/Console/Command/BuildCommand.php index 2a74af9d..d80e1b3f 100644 --- a/src/Console/Command/BuildCommand.php +++ b/src/Console/Command/BuildCommand.php @@ -177,7 +177,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int // disable packagist by default unset(Config::$defaultRepositories['packagist'], Config::$defaultRepositories['packagist.org']); - if (!$outputDir = $input->getArgument('output-dir')) { + $outputDir = $input->getArgument('output-dir'); + if (!(bool) $outputDir) { $outputDir = $config['output-dir'] ?? null; } @@ -251,8 +252,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int $packagesBuilder = new PackagesBuilder($output, $outputDir, $config, $skipErrors, $minify); $packagesBuilder->dump($packages); - if ($htmlView = !$input->getOption('no-html-output')) { - $htmlView = !isset($config['output-html']) || $config['output-html']; + $htmlView = (bool) $input->getOption('no-html-output'); + if (!$htmlView) { + $htmlView = !isset($config['output-html']) || (bool) $config['output-html']; } if ($htmlView) { diff --git a/src/Console/Command/InitCommand.php b/src/Console/Command/InitCommand.php index 2b2b6258..e69772b1 100644 --- a/src/Console/Command/InitCommand.php +++ b/src/Console/Command/InitCommand.php @@ -138,7 +138,7 @@ protected function prompt(InputInterface $input, OutputInterface $output, string protected function getQuestion(string $prompt, ?string $default): Question { - $prompt = ($default && '' !== $default ? sprintf('%s (%s)', $prompt, $default) : $prompt) . ': '; + $prompt = (is_string($default) && '' !== $default ? sprintf('%s (%s)', $prompt, $default) : $prompt) . ': '; return new Question($prompt, $default); } diff --git a/src/Console/Command/PurgeCommand.php b/src/Console/Command/PurgeCommand.php index 67ad0e15..124020bd 100644 --- a/src/Console/Command/PurgeCommand.php +++ b/src/Console/Command/PurgeCommand.php @@ -79,16 +79,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int $packageSelection = new PackageSelection($output, $outputDir, $config, false); $packages = $packageSelection->load(); + $satis_homepage = getenv('SATIS_HOMEPAGE'); $prefix = sprintf( '%s/%s/', - $config['archive']['prefix-url'] ?? getenv('SATIS_HOMEPAGE') ?: $config['homepage'], + $config['archive']['prefix-url'] ?? (false !== $satis_homepage ? $satis_homepage : $config['homepage']), $config['archive']['directory'] ); $length = strlen($prefix); $needed = []; foreach ($packages as $package) { - if (!$package->getDistType()) { + if (is_null($package->getDistType())) { continue; } $url = $package->getDistUrl(); @@ -105,7 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->in($distDirectory) ; - if (!$finder->count()) { + if (0 === $finder->count()) { $output->writeln('No archives found.'); return 0; @@ -113,27 +114,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int /** @var SplFileInfo[] $unreferenced */ $unreferenced = []; - foreach ($finder as $file) { - $filename = strtr($file->getRelativePathname(), DIRECTORY_SEPARATOR, '/'); - if (!in_array($filename, $needed)) { - $unreferenced[] = $file; + foreach ($finder as $currentFile) { + $filename = strtr($currentFile->getRelativePathname(), DIRECTORY_SEPARATOR, '/'); + if (!in_array($filename, $needed, true)) { + $unreferenced[] = $currentFile; } } - if (empty($unreferenced)) { + if (0 === count($unreferenced)) { $output->writeln('No unreferenced archives found.'); return 0; } - foreach ($unreferenced as $file) { + foreach ($unreferenced as $currentFile) { if (!$dryRun) { - unlink($file->getPathname()); + unlink($currentFile->getPathname()); } $output->writeln(sprintf( 'Removed archive: %s', - $file->getRelativePathname() + $currentFile->getRelativePathname() )); } diff --git a/src/PackageSelection/PackageSelection.php b/src/PackageSelection/PackageSelection.php index 82039ae5..0a1144d3 100644 --- a/src/PackageSelection/PackageSelection.php +++ b/src/PackageSelection/PackageSelection.php @@ -36,11 +36,11 @@ class PackageSelection { - /** @var OutputInterface The output Interface. */ - protected $output; + /** The output Interface. */ + protected OutputInterface $output; - /** @var bool Skips Exceptions if true. */ - protected $skipErrors; + /** Skips Exceptions if true. */ + protected bool $skipErrors; /** @var string packages.json file name. */ private $filename; @@ -48,65 +48,65 @@ class PackageSelection /** @var mixed Array of additional repositories for dependencies */ private $depRepositories; - /** @var bool Selects All Packages if true. */ - private $requireAll; + /** Selects All Packages if true. */ + private bool $requireAll; - /** @var bool Add required dependencies if true. */ - private $requireDependencies; + /** Add required dependencies if true. */ + private bool $requireDependencies; - /** @var bool required dev-dependencies if true. */ - private $requireDevDependencies; + /** required dev-dependencies if true. */ + private bool $requireDevDependencies; - /** @var bool do not build packages only dependencies */ - private $onlyDependencies; + /** do not build packages only dependencies */ + private bool $onlyDependencies; - /** @var bool only resolve best candidates in dependencies */ - private $onlyBestCandidates; + /** only resolve best candidates in dependencies */ + private bool $onlyBestCandidates; - /** @var bool Filter dependencies if true. */ - private $requireDependencyFilter; + /** Filter dependencies if true. */ + private bool $requireDependencyFilter; - /** @var string Minimum stability accepted for Packages in the list. */ - private $minimumStability; + /** Minimum stability accepted for Packages in the list. */ + private string $minimumStability; /** @var string[] Minimum stability accepted by Package. */ - private $minimumStabilityPerPackage; + private array $minimumStabilityPerPackage; /** @var string[] The active package filter to merge. */ - private $packagesFilter = []; + private array $packagesFilter = []; /** @var string[]|null The active repository filter to merge. */ - private $repositoriesFilter; + private ?array $repositoriesFilter = null; /** @var mixed Repositories mentioned in the satis config */ private $repositories; - /** @var bool Apply the filter also for resolving dependencies. */ - private $repositoryFilterDep; + /** Apply the filter also for resolving dependencies. */ + private bool $repositoryFilterDep; /** @var PackageInterface[] The selected packages from config */ - private $selected = []; + private array $selected = []; /** @var string[] A list of packages marked as abandoned */ - private $abandoned = []; + private array $abandoned = []; /** @var string[] A list of blacklisted package/constraints. */ - private $blacklist = []; + private array $blacklist = []; /** @var string[]|null A list of package types. If set only packages with one of these types will be selected */ - private $includeTypes; + private ?array $includeTypes; /** @var string[] A list of package types that will not be selected */ - private $excludeTypes = []; + private array $excludeTypes = []; /** @var mixed Patterns from strip-hosts. */ private $stripHosts = false; - /** @var string The prefix of the distURLs when using archive. */ - private $archiveEndpoint; + /** The prefix of the distURLs when using archive. */ + private ?string $archiveEndpoint = null; - /** @var string The homepage - needed to get the relative paths of the providers */ - private $homepage; + /** The homepage - needed to get the relative paths of the providers */ + private ?string $homepage = null; /** * @param array $config @@ -126,7 +126,7 @@ public function __construct(OutputInterface $output, string $outputDir, array $c public function setRepositoriesFilter(?array $repositoriesFilter, bool $forDependencies = false): void { $this->repositoriesFilter = [] !== $repositoriesFilter ? $repositoriesFilter : null; - $this->repositoryFilterDep = (bool) $forDependencies; + $this->repositoryFilterDep = $forDependencies; } public function hasRepositoriesFilter(): bool @@ -296,13 +296,13 @@ public function load(): array } if (isset($rootConfig['providers']) && is_array($rootConfig['providers']) && isset($rootConfig['providers-url'])) { - $baseUrl = $this->homepage ? parse_url(rtrim($this->homepage, '/'), PHP_URL_PATH) . '/' : ''; + $baseUrl = is_string($this->homepage) ? parse_url(rtrim($this->homepage, '/'), PHP_URL_PATH) . '/' : ''; $baseUrlLength = strlen($baseUrl); foreach ($rootConfig['providers'] as $package => $provider) { $file = (string) str_replace(['%package%', '%hash%'], [$package, $provider['sha256']], $rootConfig['providers-url']); - if ($baseUrl && substr($file, 0, $baseUrlLength) === $baseUrl) { + if (strlen($baseUrl) > 0 && substr($file, 0, $baseUrlLength) === $baseUrl) { $file = substr($file, $baseUrlLength); } @@ -341,7 +341,7 @@ public function load(): array continue; } - if (isset($package['name']) && in_array($package['name'], $this->packagesFilter)) { + if (isset($package['name']) && in_array($package['name'], $this->packagesFilter, true)) { continue; } @@ -405,7 +405,7 @@ private function createStripHostsPatterns($stripHostsConfig) $patterns = []; foreach ($stripHostsConfig as $entry) { - if (!strlen($entry)) { + if (0 === strlen($entry)) { continue; } @@ -464,11 +464,11 @@ private function applyStripHosts(): void foreach ($this->selected as $uniqueName => $package) { $sources = []; - if ($package->getSourceType()) { + if (is_string($package->getSourceType())) { $sources[] = 'source'; } - if ($package->getDistType()) { + if (is_string($package->getDistType())) { $sources[] = 'dist'; } @@ -654,7 +654,7 @@ private function pruneByType(bool $verbose): array $excluded = []; if ($this->hasTypeFilter()) { foreach ($this->selected as $selectedKey => $package) { - if (null !== $this->includeTypes && !in_array($package->getType(), $this->includeTypes)) { + if (null !== $this->includeTypes && !in_array($package->getType(), $this->includeTypes, true)) { if ($verbose) { $this->output->writeln( 'Excluded ' . $package->getPrettyName() @@ -664,7 +664,7 @@ private function pruneByType(bool $verbose): array } $excluded[$selectedKey] = $package; unset($this->selected[$selectedKey]); - } elseif (in_array($package->getType(), $this->excludeTypes)) { + } elseif (in_array($package->getType(), $this->excludeTypes, true)) { if ($verbose) { $this->output->writeln( 'Excluded ' . $package->getPrettyName() @@ -698,7 +698,7 @@ private function getFilteredLinks(PartialComposer $composer): array $links = array_filter( $links, function (Link $link) use ($packagesFilter) { - return in_array($link->getTarget(), $packagesFilter); + return in_array($link->getTarget(), $packagesFilter, true); } ); @@ -946,7 +946,7 @@ static function ($repository) use ($packages) { $config = $repository->getRepoConfig(); // We need name to be set on repo config as it would otherwise be too slow on remote repos (VCS, ..) - if (!isset($config['name']) || !in_array($config['name'], $packages)) { + if (!isset($config['name']) || !in_array($config['name'], $packages, true)) { return false; } diff --git a/tests/Builder/ArchiveBuilderHelperTest.php b/tests/Builder/ArchiveBuilderHelperTest.php index 41667695..d91e2b37 100644 --- a/tests/Builder/ArchiveBuilderHelperTest.php +++ b/tests/Builder/ArchiveBuilderHelperTest.php @@ -61,7 +61,7 @@ public function dataDirectories(): array public function testDirectoryConfig(string $expected, string $outputDir, array $config): void { $helper = new ArchiveBuilderHelper($this->output, $config); - $this->assertEquals($helper->getDirectory($outputDir), $expected); + self::assertEquals($helper->getDirectory($outputDir), $expected); } /** @@ -138,6 +138,6 @@ public function dataPackages(): array public function testSkipDump(bool $expected, Package $package, array $config): void { $helper = new ArchiveBuilderHelper($this->output, $config); - $this->assertEquals($helper->isSkippable($package), $expected); + self::assertEquals($helper->isSkippable($package), $expected); } } diff --git a/tests/Builder/ArchiveBuilderTest.php b/tests/Builder/ArchiveBuilderTest.php index 9de54158..c111ac1c 100644 --- a/tests/Builder/ArchiveBuilderTest.php +++ b/tests/Builder/ArchiveBuilderTest.php @@ -74,7 +74,7 @@ public function setUp(): void $downloadManager = $this->getMockBuilder(DownloadManager::class)->disableOriginalConstructor()->getMock(); $downloadManager->method('download')->will( - $this->returnCallback( + self::returnCallback( function ($package, $source) { $filesystem = new Filesystem(); $filesystem->dumpFile(realpath($source) . '/README.md', '# The demo archive.'); @@ -102,7 +102,7 @@ public function archive(CompletePackageInterface $package, string $format, strin $this->composer->setArchiveManager($archiveManager); $this->input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->disableOriginalConstructor()->getMock(); - $this->input->method('getOption')->with('stats')->willReturn($this->returnValue(false)); + $this->input->method('getOption')->with('stats')->willReturn(self::returnValue(false)); $this->output = new NullOutput(); @@ -148,7 +148,7 @@ public function testDumpWithDownloadedArchives(array $customConfig, array $packa $builder->setComposer($this->composer); $builder->dump($packages); - $this->assertSame($expectedFileName, basename($packages[0]->getDistUrl())); + self::assertSame($expectedFileName, basename($packages[0]->getDistUrl())); } /** @@ -206,7 +206,7 @@ public function testDumpWithoutDownloadedArchives(array $customConfig, array $pa $builder->setComposer($this->composer); $builder->dump($packages); - $this->assertSame($expectedFileName, basename($packages[0]->getDistUrl())); + self::assertSame($expectedFileName, basename($packages[0]->getDistUrl())); } private function initArchives(): void diff --git a/tests/Builder/PackagesBuilderDumpTest.php b/tests/Builder/PackagesBuilderDumpTest.php index d79fa835..e1231cf4 100644 --- a/tests/Builder/PackagesBuilderDumpTest.php +++ b/tests/Builder/PackagesBuilderDumpTest.php @@ -76,7 +76,7 @@ public function testNominalCase(bool $providers = false): void /** @var vfsStreamFile $file */ $file = $this->root->getChild('build/packages.json'); $packagesJson = JsonFile::parseJson($file->getContent()); - $this->assertArrayNotHasKey('notify-batch', $packagesJson); + self::assertArrayNotHasKey('notify-batch', $packagesJson); if ($providers) { $packageName = key($arrayPackages); @@ -94,25 +94,25 @@ public function testNominalCase(bool $providers = false): void } $includeJsonFile = 'build/' . $includeJson; - $this->assertTrue(is_file(vfsStream::url($includeJsonFile))); + self::assertTrue(is_file(vfsStream::url($includeJsonFile))); /** @var vfsStreamFile $file */ $file = $this->root->getChild($includeJsonFile); $packagesIncludeJson = JsonFile::parseJson($file->getContent()); - $this->assertEquals($arrayPackages, $packagesIncludeJson['packages']); + self::assertEquals($arrayPackages, $packagesIncludeJson['packages']); if (!is_null($lastIncludedJsonFile) && $lastIncludedJsonFile !== $includeJsonFile) { - $this->assertFalse(is_file(vfsStream::url($lastIncludedJsonFile)), 'Previous files not pruned'); + self::assertFalse(is_file(vfsStream::url($lastIncludedJsonFile)), 'Previous files not pruned'); } $lastIncludedJsonFile = $includeJsonFile; - $this->assertArrayHasKey('metadata-url', $packagesJson); + self::assertArrayHasKey('metadata-url', $packagesJson); $packageName = key($arrayPackages); foreach (['', '~dev'] as $suffix) { $includeJson = str_replace('%package%', $packageName.$suffix, $packagesJson['metadata-url']); $includeJsonFile = 'build/' . $includeJson; - $this->assertTrue(is_file(vfsStream::url($includeJsonFile)), $includeJsonFile.' file must be created'); + self::assertTrue(is_file(vfsStream::url($includeJsonFile)), $includeJsonFile.' file must be created'); } } } @@ -143,10 +143,10 @@ public function testProvidersUrl(): void /** @var vfsStreamFile $file */ $file = $this->root->getChild('build/packages.json'); $packagesJson = JsonFile::parseJson($file->getContent()); - if (!$basePath) { + if (is_null($basePath)) { $providersUrlWithoutBase = $packagesJson['providers-url']; } else { - $this->assertEquals($basePath . $providersUrlWithoutBase, $packagesJson['providers-url']); + self::assertEquals($basePath . $providersUrlWithoutBase, $packagesJson['providers-url']); } } } @@ -165,7 +165,7 @@ public function testNotifyBatch(): void $file = $this->root->getChild('build/packages.json'); $packagesJson = JsonFile::parseJson($file->getContent()); - $this->assertEquals('http://localhost:54715/notify', $packagesJson['notify-batch']); + self::assertEquals('http://localhost:54715/notify', $packagesJson['notify-batch']); } /** diff --git a/tests/Builder/WebBuilderDumpTest.php b/tests/Builder/WebBuilderDumpTest.php index 9b02d32a..c9e726f6 100644 --- a/tests/Builder/WebBuilderDumpTest.php +++ b/tests/Builder/WebBuilderDumpTest.php @@ -64,9 +64,9 @@ public function testNominalCase(): void $file = $this->root->getChild('build/index.html'); $html = $file->getContent(); - $this->assertMatchesRegularExpression('/dummy root package<\/title>/', $html); - $this->assertMatchesRegularExpression('{<div id="[^"]+" class="card-header[^"]+">\s*<a href="#vendor/name" class="[^"]+">\s*<svg[^>]*>.+</svg>\s*vendor/name\s*</a>\s*</div>}si', $html); - $this->assertFalse((bool) preg_match('/<p class="abandoned">/', $html)); + self::assertMatchesRegularExpression('/<title>dummy root package<\/title>/', $html); + self::assertMatchesRegularExpression('{<div id="[^"]+" class="card-header[^"]+">\s*<a href="#vendor/name" class="[^"]+">\s*<svg[^>]*>.+</svg>\s*vendor/name\s*</a>\s*</div>}si', $html); + self::assertFalse((bool) preg_match('/<p class="abandoned">/', $html)); } public function testRepositoryWithNoName(): void @@ -80,7 +80,7 @@ public function testRepositoryWithNoName(): void $file = $this->root->getChild('build/index.html'); $html = $file->getContent(); - $this->assertMatchesRegularExpression('/<title>A<\/title>/', $html); + self::assertMatchesRegularExpression('/<title>A<\/title>/', $html); } public function testDependencies(): void @@ -95,7 +95,7 @@ public function testDependencies(): void $file = $this->root->getChild('build/index.html'); $html = $file->getContent(); - $this->assertMatchesRegularExpression('/<a href="#dummytest">dummytest<\/a>/', $html); + self::assertMatchesRegularExpression('/<a href="#dummytest">dummytest<\/a>/', $html); } /** @@ -134,7 +134,7 @@ public function testAbandoned($abandoned, string $expected): void $file = $this->root->getChild('build/index.html'); $html = $file->getContent(); - $this->assertMatchesRegularExpression('/Package is abandoned, you should avoid using it/', $html); - $this->assertMatchesRegularExpression($expected, $html); + self::assertMatchesRegularExpression('/Package is abandoned, you should avoid using it/', $html); + self::assertMatchesRegularExpression($expected, $html); } } diff --git a/tests/Builder/WebBuilderTest.php b/tests/Builder/WebBuilderTest.php index 3772f9f0..a07a6359 100644 --- a/tests/Builder/WebBuilderTest.php +++ b/tests/Builder/WebBuilderTest.php @@ -70,7 +70,7 @@ public function testTwigEnvironment(): void $method = $reflection->getMethod('getTwigEnvironment'); $method->setAccessible(true); - $this->assertSame($twig, $method->invoke($this->webBuilder)); + self::assertSame($twig, $method->invoke($this->webBuilder)); } public function testTwigEnvironmentDefault(): void @@ -79,7 +79,7 @@ public function testTwigEnvironmentDefault(): void $method = $reflection->getMethod('getTwigEnvironment'); $method->setAccessible(true); - $this->assertInstanceOf('\Twig\Environment', $method->invoke($this->webBuilder)); + self::assertInstanceOf('\Twig\Environment', $method->invoke($this->webBuilder)); } public function testTwigTemplate(): void @@ -92,7 +92,7 @@ public function testTwigTemplate(): void $method = $reflection->getMethod('getTwigTemplate'); $method->setAccessible(true); - $this->assertSame('foo.twig', $method->invoke($this->webBuilder)); + self::assertSame('foo.twig', $method->invoke($this->webBuilder)); } public function testTwigTemplateDefault(): void @@ -101,7 +101,7 @@ public function testTwigTemplateDefault(): void $method = $reflection->getMethod('getTwigTemplate'); $method->setAccessible(true); - $this->assertSame('index.html.twig', $method->invoke($this->webBuilder)); + self::assertSame('index.html.twig', $method->invoke($this->webBuilder)); } /** @@ -116,7 +116,7 @@ public function testGetDescSortedVersions(array $expected, array $packages): voi $method = $reflection->getMethod('getDescSortedVersions'); $method->setAccessible(true); - $this->assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); + self::assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); } /** @@ -152,7 +152,7 @@ public function testGetHighestVersion(Package $expected, array $packages): void $method = $reflection->getMethod('getHighestVersion'); $method->setAccessible(true); - $this->assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); + self::assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); } /** @@ -198,6 +198,6 @@ public function testGroupPackagesByName(array $expected, array $packages): void $method = $reflection->getMethod('groupPackagesByName'); $method->setAccessible(true); - $this->assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); + self::assertEquals($expected, $method->invokeArgs($this->webBuilder, $packages)); } } diff --git a/tests/PackageSelection/PackageSelectionLoadTest.php b/tests/PackageSelection/PackageSelectionLoadTest.php index 106e0e7b..6daa3053 100644 --- a/tests/PackageSelection/PackageSelectionLoadTest.php +++ b/tests/PackageSelection/PackageSelectionLoadTest.php @@ -87,7 +87,7 @@ public function testNoJsonFile(): void * no json filename means empty $packages */ $this->root->removeChild('packages.json'); - $this->assertEmpty($this->selection->load()); + self::assertEmpty($this->selection->load()); } public function testNoIncludeFile(): void @@ -96,7 +96,7 @@ public function testNoIncludeFile(): void * include file not found means output + empty $packages */ $this->root->removeChild('include'); - $this->assertEmpty($this->selection->load()); + self::assertEmpty($this->selection->load()); } public function testNoPackagesFilter(): void @@ -105,7 +105,7 @@ public function testNoPackagesFilter(): void * no filterPackages means all $packages */ $this->selection->setPackagesFilter([]); - $this->assertNotEmpty($this->selection->load()); + self::assertNotEmpty($this->selection->load()); } public function testPackageInFilter(): void @@ -114,7 +114,7 @@ public function testPackageInFilter(): void * json filename + filterPackages : * package in json + in filter => not selected (because it'll replaced/updated) */ - $this->assertEmpty($this->selection->load()); + self::assertEmpty($this->selection->load()); } public function testPackageNotInFilter(): void @@ -124,22 +124,22 @@ public function testPackageNotInFilter(): void * package in json + not in filter => selected (to be merged as is) */ $this->selection->setPackagesFilter(['othervendor/othername']); - $this->assertNotEmpty($this->selection->load()); + self::assertNotEmpty($this->selection->load()); } public function testAliasNotSelected(): void { $this->selection->setPackagesFilter(['othervendor/othername']); $packages = $this->selection->load(); - $this->assertNotEmpty($packages); + self::assertNotEmpty($packages); foreach ($packages as $package) { - $this->assertNotInstanceOf(AliasPackage::class, $package); + self::assertNotInstanceOf(AliasPackage::class, $package); if ($package->isDev()) { - $this->assertSame('dev-master', $package->getPrettyVersion()); + self::assertSame('dev-master', $package->getPrettyVersion()); } else { - $this->assertSame('1.0', $package->getPrettyVersion()); + self::assertSame('1.0', $package->getPrettyVersion()); } } } diff --git a/tests/PackageSelection/PackageSelectionTest.php b/tests/PackageSelection/PackageSelectionTest.php index 5f2ad581..e33895ca 100644 --- a/tests/PackageSelection/PackageSelectionTest.php +++ b/tests/PackageSelection/PackageSelectionTest.php @@ -99,7 +99,7 @@ public function dataGetPackages(): array public function testGetPackages(array $expected, array $filter, ArrayRepository $repository): void { $builder = new PackageSelection(new NullOutput(), 'build', [], false); - if (!empty($filter)) { + if (count($filter) > 0) { $builder->setPackagesFilter($filter); } @@ -107,7 +107,7 @@ public function testGetPackages(array $expected, array $filter, ArrayRepository $method = $reflection->getMethod('getPackages'); $method->setAccessible(true); - $this->assertSame($expected, $method->invokeArgs($builder, [$repository])); + self::assertSame($expected, $method->invokeArgs($builder, [$repository])); } /** @@ -175,7 +175,7 @@ public function testGetRequired(array $expected, Package $package, bool $require $property->setAccessible(true); $property->setValue($builder, $requireDevDependencies); - $this->assertSame($expected, $method->invokeArgs($builder, [$package, true])); + self::assertSame($expected, $method->invokeArgs($builder, [$package, true])); } /** @@ -233,7 +233,7 @@ public function testSetSelectedAsAbandoned(array $expected, array $config): void $method->invokeArgs($builder, []); - $this->assertEquals($expected, $property->getValue($builder)); + self::assertEquals($expected, $property->getValue($builder)); } /** @@ -293,7 +293,7 @@ public function testPruneBlacklisted(array $expected, array $selected, array $co $method->setAccessible(true); $method->invokeArgs($builder, [$repositorySet, false]); - $this->assertEquals(array_values($expected), array_values($property->getValue($builder))); + self::assertEquals(array_values($expected), array_values($property->getValue($builder))); } /** @@ -378,6 +378,7 @@ public function dataSelect(): array ], ]; + $repo = []; $repo['alpha'] = [ 'type' => 'package', 'url' => 'example.org/project-alpha', @@ -763,7 +764,7 @@ public function testSelect(array $expected, array $config, ?array $filterRepos = $selection = new PackageSelection(new NullOutput(), 'build', $config, false); $selection->setRepositoriesFilter($filterRepos); - $selection->setPackagesFilter($filterPackages ?? []); + $selection->setPackagesFilter([]); $selection->select($composer, true); @@ -772,7 +773,7 @@ public function testSelect(array $expected, array $config, ?array $filterRepos = $selected->setAccessible(true); \sort($expected, \SORT_STRING); - $this->assertEquals($expected, \array_keys($selected->getValue($selection))); + self::assertEquals($expected, \array_keys($selected->getValue($selection))); } /** @@ -822,7 +823,7 @@ public function testMetadataSupport(array $expected, ArrayRepository $repository $select->setAccessible(true); $result = $select->invokeArgs($selection, [$composer, true]); - $this->assertEquals($expected, array_keys($result)); + self::assertEquals($expected, array_keys($result)); } /** @@ -883,6 +884,7 @@ public function dataClean(): array ], ]; + $data = []; $data['Keep everything'] = [ [ 'alpha' => ['http://127.0.0.1/output/dist/alpha.zip', './git-repo'], @@ -1033,7 +1035,7 @@ public function testClean(array $expected, array $config, array $packages): void ]; } - $this->assertEquals($expected, $sources); + self::assertEquals($expected, $sources); } public function testOnlyBestCandidates(): void @@ -1082,7 +1084,7 @@ public function testOnlyBestCandidates(): void $property = $reflection->getProperty('selected'); $property->setAccessible(true); - $this->assertEquals(array_values([$packageA0, $packageB1, $packageC1]), array_values($property->getValue($builder))); + self::assertEquals(array_values([$packageA0, $packageB1, $packageC1]), array_values($property->getValue($builder))); } }