From 0a8c886e748d32ffa259e486b939a4ec66ba36fc Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 17:44:18 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- src/Console/VendorExposeCommand.php | 16 ++++++++-------- src/Library.php | 22 +++++++++++----------- src/Methods/CopyMethod.php | 6 +++--- src/Methods/SymlinkMethod.php | 4 ++-- src/Util.php | 4 ++-- src/VendorExposeTask.php | 10 +++++----- src/VendorModule.php | 2 +- src/VendorPlugin.php | 6 +++--- tests/Methods/ChainedMethodTest.php | 4 ++-- tests/Methods/CopyMethodTest.php | 4 ++-- tests/Methods/SymlinkMethodTest.php | 4 ++-- 11 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/Console/VendorExposeCommand.php b/src/Console/VendorExposeCommand.php index e55082d..46aedf5 100644 --- a/src/Console/VendorExposeCommand.php +++ b/src/Console/VendorExposeCommand.php @@ -73,7 +73,7 @@ protected function getAllLibraries() foreach ($this->getModulePaths() as $modulePath) { // Filter by non-composer folders $composerPath = Util::joinPaths($modulePath, 'composer.json'); - if (!file_exists($composerPath)) { + if (!file_exists($composerPath ?? '')) { continue; } @@ -113,21 +113,21 @@ protected function getModulePaths() // Get vendor modules $search = Util::joinPaths($basePath, 'vendor', '*', '*'); - foreach (glob($search, GLOB_ONLYDIR) as $modulePath) { + foreach (glob($search ?? '', GLOB_ONLYDIR) as $modulePath) { if ($this->isPathModule($modulePath)) { yield $modulePath; } } // Check if public/ folder exists - $publicExists = is_dir(Util::joinPaths($basePath, Library::PUBLIC_PATH)); + $publicExists = is_dir(Util::joinPaths($basePath, Library::PUBLIC_PATH) ?? ''); if (!$publicExists) { return; } // Search all base folders / modules $search = Util::joinPaths($basePath, '*'); - foreach (glob($search, GLOB_ONLYDIR) as $modulePath) { + foreach (glob($search ?? '', GLOB_ONLYDIR) as $modulePath) { if ($this->isPathModule($modulePath)) { yield $modulePath; } @@ -135,7 +135,7 @@ protected function getModulePaths() // Check all themes $search = Util::joinPaths($basePath, 'themes', '*'); - foreach (glob($search, GLOB_ONLYDIR) as $themePath) { + foreach (glob($search ?? '', GLOB_ONLYDIR) as $themePath) { yield $themePath; } } @@ -148,8 +148,8 @@ protected function getModulePaths() */ protected function isPathModule($path) { - return file_exists(Util::joinPaths($path, '_config')) - || file_exists(Util::joinPaths($path, '_config.php')); + return file_exists(Util::joinPaths($path, '_config') ?? '') + || file_exists(Util::joinPaths($path, '_config.php') ?? ''); } /** @@ -157,6 +157,6 @@ protected function isPathModule($path) */ protected function getProjectPath() { - return dirname(realpath(Factory::getComposerFile())); + return dirname(realpath(Factory::getComposerFile() ?? '') ?? ''); } } diff --git a/src/Library.php b/src/Library.php index 8b4a073..9e00fa9 100644 --- a/src/Library.php +++ b/src/Library.php @@ -56,8 +56,8 @@ public function __construct( $libraryPath, $name = null ) { - $this->basePath = realpath($basePath); - $this->path = realpath($libraryPath); + $this->basePath = realpath($basePath ?? ''); + $this->path = realpath($libraryPath ?? ''); $this->name = $name; } @@ -146,7 +146,7 @@ public function getPath() */ public function getRelativePath() { - return trim(substr($this->path, strlen($this->basePath)), self::TRIM_CHARS); + return trim(substr($this->path ?? '', strlen($this->basePath ?? '')), self::TRIM_CHARS ?? ''); } /** @@ -161,7 +161,7 @@ public function getPublicPath() // 4.0 compatibility: If there is no public folder, and this is a vendor path, // remove the leading `vendor` from the destination if (!$this->publicPathExists() && $this->installedIntoVendor()) { - $relativePath = substr($relativePath, strlen('vendor/')); + $relativePath = substr($relativePath ?? '', strlen('vendor/')); } return Util::joinPaths($this->getBasePublicPath(), $relativePath); @@ -185,7 +185,7 @@ protected function getJson() return $this->json; } $composer = Util::joinPaths($this->getPath(), 'composer.json'); - if (!file_exists($composer)) { + if (!file_exists($composer ?? '')) { return []; } $file = new JsonFile($composer); @@ -264,13 +264,13 @@ public function getExposedFolders() */ protected function validateFolder($exposeFolder) { - if (strstr($exposeFolder, '.')) { + if (strstr($exposeFolder ?? '', '.')) { return false; } - if (strpos($exposeFolder, '/') === 0) { + if (strpos($exposeFolder ?? '', '/') === 0) { return false; } - if (strpos($exposeFolder, '\\') === 0) { + if (strpos($exposeFolder ?? '', '\\') === 0) { return false; } return true; @@ -283,7 +283,7 @@ protected function validateFolder($exposeFolder) */ public function publicPathExists() { - return is_dir(Util::joinPaths($this->getBasePath(), self::PUBLIC_PATH)); + return is_dir(Util::joinPaths($this->getBasePath(), self::PUBLIC_PATH) ?? ''); } /** @@ -293,7 +293,7 @@ public function publicPathExists() */ protected function installedIntoVendor() { - return preg_match('#^vendor[/\\\\]#', $this->getRelativePath()); + return preg_match('#^vendor[/\\\\]#', $this->getRelativePath() ?? ''); } /** @@ -316,7 +316,7 @@ public function getResourcesDir() : self::DEFAULT_RESOURCES_DIR; - if (preg_match('/^[_\-a-z0-9]+$/i', $resourcesDir)) { + if (preg_match('/^[_\-a-z0-9]+$/i', $resourcesDir ?? '')) { return $resourcesDir; } diff --git a/src/Methods/CopyMethod.php b/src/Methods/CopyMethod.php index 80896c6..c38d05a 100644 --- a/src/Methods/CopyMethod.php +++ b/src/Methods/CopyMethod.php @@ -46,8 +46,8 @@ public function exposeDirectory($source, $target) */ public function copy($source, $target) { - if (!is_dir($source)) { - return copy($source, $target); + if (!is_dir($source ?? '')) { + return copy($source ?? '', $target ?? ''); } $it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS); /** @var RecursiveDirectoryIterator $ri */ @@ -59,7 +59,7 @@ public function copy($source, $target) if ($file->isDir()) { $this->filesystem->ensureDirectoryExists($targetPath); } else { - $result = $result && copy($file->getPathname(), $targetPath); + $result = $result && copy($file->getPathname() ?? '', $targetPath ?? ''); } } return $result; diff --git a/src/Methods/SymlinkMethod.php b/src/Methods/SymlinkMethod.php index 752b734..bfe9a66 100644 --- a/src/Methods/SymlinkMethod.php +++ b/src/Methods/SymlinkMethod.php @@ -25,13 +25,13 @@ public function __construct(Filesystem $filesystem = null) public function exposeDirectory($source, $target) { // Remove trailing slash - $target = rtrim($target, DIRECTORY_SEPARATOR); + $target = rtrim($target ?? '', DIRECTORY_SEPARATOR); // Remove destination directory to ensure it is clean $this->filesystem->removeDirectory($target); // Ensure parent dir exist - $parent = dirname($target); + $parent = dirname($target ?? ''); $this->filesystem->ensureDirectoryExists($parent); // Ensure symlink exists diff --git a/src/Util.php b/src/Util.php index 6f47e8a..631aa8f 100644 --- a/src/Util.php +++ b/src/Util.php @@ -13,10 +13,10 @@ class Util public static function joinPaths(...$parts) { $combined = null; - $parts = array_filter($parts); + $parts = array_filter($parts ?? []); array_walk_recursive($parts, function ($part) use (&$combined) { // Normalise path - $part = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $part); + $part = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $part ?? ''); $combined = $combined ? (rtrim($combined, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $part) : $part; diff --git a/src/VendorExposeTask.php b/src/VendorExposeTask.php index 450b8aa..2456ddd 100644 --- a/src/VendorExposeTask.php +++ b/src/VendorExposeTask.php @@ -120,10 +120,10 @@ protected function setupResources(IOInterface $io) $files = new DirectoryIterator(__DIR__.'/../resources'); foreach ($files as $file) { $targetPath = $resourcesPath . DIRECTORY_SEPARATOR . $file->getFilename(); - if ($file->isFile() && !file_exists($targetPath)) { + if ($file->isFile() && !file_exists($targetPath ?? '')) { $name = $file->getFilename(); $io->write("Writing {$name} to resources folder"); - copy($file->getPathname(), $targetPath); + copy($file->getPathname() ?? '', $targetPath ?? ''); } } } @@ -169,8 +169,8 @@ protected function getMethodKey() { // Switch if `resources/.method` contains a file $methodFilePath = $this->getMethodFilePath(); - if (file_exists($methodFilePath) && is_readable($methodFilePath)) { - return trim(file_get_contents($methodFilePath)); + if (file_exists($methodFilePath ?? '') && is_readable($methodFilePath ?? '')) { + return trim(file_get_contents($methodFilePath ?? '') ?? ''); } // Switch based on SS_VENDOR_METHOD arg @@ -191,7 +191,7 @@ protected function getMethodKey() protected function saveMethodKey($key) { $methodFilePath = $this->getMethodFilePath(); - file_put_contents($methodFilePath, $key); + file_put_contents($methodFilePath ?? '', $key); } /** diff --git a/src/VendorModule.php b/src/VendorModule.php index 6e4b2e6..628ea12 100644 --- a/src/VendorModule.php +++ b/src/VendorModule.php @@ -30,7 +30,7 @@ public function __construct($basePath, $name) $path = Util::joinPaths( $basePath, static::DEFAULT_SOURCE, - explode('/', $name) + explode('/', $name ?? '') ); parent::__construct($basePath, $path, $name); } diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 0fbd46c..e67b27b 100644 --- a/src/VendorPlugin.php +++ b/src/VendorPlugin.php @@ -166,7 +166,7 @@ public function installRootPackage(Event $event) */ protected function getProjectPath() { - return dirname(realpath(Factory::getComposerFile())); + return dirname(realpath(Factory::getComposerFile() ?? '') ?? ''); } /** @@ -184,7 +184,7 @@ public function uninstallPackage(PackageEvent $event) // Check path to remove $target = $library->getPublicPath(); - if (!is_dir($target)) { + if (!is_dir($target ?? '')) { return; } @@ -194,7 +194,7 @@ public function uninstallPackage(PackageEvent $event) $this->filesystem->removeDirectory($target); // Cleanup empty vendor dir if this is the last module - $targetParent = dirname($target); + $targetParent = dirname($target ?? ''); if ($this->filesystem->isDirEmpty($targetParent)) { $this->filesystem->removeDirectory($targetParent); } diff --git a/tests/Methods/ChainedMethodTest.php b/tests/Methods/ChainedMethodTest.php index 86c7e87..093f17d 100644 --- a/tests/Methods/ChainedMethodTest.php +++ b/tests/Methods/ChainedMethodTest.php @@ -69,7 +69,7 @@ public function testFailover() // Parent folder is a real folder - $this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target))); - $this->assertDirectoryExists(dirname($target)); + $this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target ?? ''))); + $this->assertDirectoryExists(dirname($target ?? '')); } } diff --git a/tests/Methods/CopyMethodTest.php b/tests/Methods/CopyMethodTest.php index dd3fe9c..962f58b 100644 --- a/tests/Methods/CopyMethodTest.php +++ b/tests/Methods/CopyMethodTest.php @@ -60,8 +60,8 @@ public function testCopy() // Parent folder is a real folder - $this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target))); - $this->assertDirectoryExists(dirname($target)); + $this->assertFalse($this->filesystem->isSymlinkedDirectory(dirname($target ?? ''))); + $this->assertDirectoryExists(dirname($target ?? '')); } public function testRecoversFromSymlink() diff --git a/tests/Methods/SymlinkMethodTest.php b/tests/Methods/SymlinkMethodTest.php index 6c87f5d..caf5445 100644 --- a/tests/Methods/SymlinkMethodTest.php +++ b/tests/Methods/SymlinkMethodTest.php @@ -63,7 +63,7 @@ public function testSymlink() } // Parent folder is a real folder - $this->assertDirectoryExists(dirname($target)); + $this->assertDirectoryExists(dirname($target ?? '')); } public function testSymlinkTrailingSlash() @@ -86,7 +86,7 @@ public function testSymlinkTrailingSlash() } // Parent folder is a real folder - $this->assertDirectoryExists(dirname($target)); + $this->assertDirectoryExists(dirname($target ?? '')); } public function testRecoversFromCopy()