From 02f5008b46294b016488b247581478b4f233ca45 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 17:43:58 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- src/RecipeCommandBehaviour.php | 6 +++--- src/RecipeInstaller.php | 30 ++++++++++++++++-------------- src/RecipePlugin.php | 2 -- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/RecipeCommandBehaviour.php b/src/RecipeCommandBehaviour.php index 8354ac5..3ee5c04 100644 --- a/src/RecipeCommandBehaviour.php +++ b/src/RecipeCommandBehaviour.php @@ -103,17 +103,17 @@ protected function findBestConstraint($existingVersion) } // Existing version is already a ^1.0.0 or ~1.0.0 constraint - if (preg_match('#^[~^]#', $existingVersion)) { + if (preg_match('#^[~^]#', $existingVersion ?? '')) { return $existingVersion; } // Existing version is already a dev constraint - if (stristr($existingVersion, 'dev') !== false) { + if (stristr($existingVersion ?? '', 'dev') !== false) { return $existingVersion; } // Numeric-only version maps to semver constraint - if (preg_match('#^([\d.]+)$#', $existingVersion)) { + if (preg_match('#^([\d.]+)$#', $existingVersion ?? '')) { return "^{$existingVersion}"; } diff --git a/src/RecipeInstaller.php b/src/RecipeInstaller.php index 767c660..5889094 100644 --- a/src/RecipeInstaller.php +++ b/src/RecipeInstaller.php @@ -61,7 +61,7 @@ protected function installProjectFiles( $relativePath = $this->installProjectFile($sourceRoot, $destinationRoot, $path, $installedFiles); // Add file to installed (even if already exists) - if (!in_array($relativePath, $installedFiles)) { + if (!in_array($relativePath, $installedFiles ?? [])) { $installedFiles[] = $relativePath; } } @@ -87,15 +87,15 @@ protected function installProjectFiles( protected function installProjectFile($sourceRoot, $destinationRoot, $sourcePath, $installedFiles) { // Relative path - $relativePath = substr($sourcePath, strlen($sourceRoot) + 1); // Name path without leading '/' + $relativePath = substr($sourcePath ?? '', strlen($sourceRoot ?? '') + 1); // Name path without leading '/' // Get destination path $relativeDestination = $this->rewriteFilePath($destinationRoot, $relativePath); $destination = $destinationRoot . DIRECTORY_SEPARATOR . $relativeDestination; // Check if file exists - if (file_exists($destination)) { - if (file_get_contents($destination) === file_get_contents($sourcePath)) { + if (file_exists($destination ?? '')) { + if (file_get_contents($destination ?? '') === file_get_contents($sourcePath ?? '')) { $this->io->write( " - Skipping $relativePath (existing, but unchanged)" ); @@ -104,15 +104,17 @@ protected function installProjectFile($sourceRoot, $destinationRoot, $sourcePath " - Skipping $relativePath (existing and modified in project)" ); } - } elseif (in_array($relativePath, $installedFiles) || in_array($relativeDestination, $installedFiles)) { + } elseif (in_array($relativePath, $installedFiles ?? []) || + in_array($relativeDestination, $installedFiles ?? []) + ) { // Don't re-install previously installed files that have been deleted $this->io->write( " - Skipping $relativePath (previously installed)" ); } else { $this->io->write(" - Copying $relativePath"); - $this->filesystem->ensureDirectoryExists(dirname($destination)); - copy($sourcePath, $destination); + $this->filesystem->ensureDirectoryExists(dirname($destination ?? '')); + copy($sourcePath ?? '', $destination ?? ''); } return $relativePath; } @@ -155,10 +157,10 @@ protected function getFileIterator($sourceRoot, $patterns) */ protected function globToRegexp($glob) { - $sourceParts = explode('*', $glob); + $sourceParts = explode('*', $glob ?? ''); $regexParts = array_map(function ($part) { - return preg_quote($part, '#'); - }, $sourceParts); + return preg_quote($part ?? '', '#'); + }, $sourceParts ?? []); return implode('(.+)', $regexParts); } @@ -176,11 +178,11 @@ public function installLibrary(PackageInterface $package) $recipePath = $this->getInstallPath($package); // Find project path - $projectPath = dirname(realpath(Factory::getComposerFile())); + $projectPath = dirname(realpath(Factory::getComposerFile() ?? '') ?? ''); // Find public path $candidatePublicPath = $projectPath . DIRECTORY_SEPARATOR . RecipePlugin::PUBLIC_PATH; - $publicPath = is_dir($candidatePublicPath) ? $candidatePublicPath : $projectPath; + $publicPath = is_dir($candidatePublicPath ?? '') ? $candidatePublicPath : $projectPath; // Copy project files to root $name = $package->getName(); @@ -238,8 +240,8 @@ protected function rewriteFilePath($destinationRoot, $relativePath) 'app' => 'mysite', ]; foreach ($rewrites as $from => $to) { - if (stripos($relativePath, $from) === 0) { - return $to . substr($relativePath, strlen($from)); + if (stripos($relativePath ?? '', $from ?? '') === 0) { + return $to . substr($relativePath ?? '', strlen($from ?? '')); } } return $relativePath; diff --git a/src/RecipePlugin.php b/src/RecipePlugin.php index ecdc3fe..fd65c3c 100644 --- a/src/RecipePlugin.php +++ b/src/RecipePlugin.php @@ -137,11 +137,9 @@ public function getCapabilities() public function deactivate(Composer $composer, IOInterface $io) { - } public function uninstall(Composer $composer, IOInterface $io) { - } }