From ce845b27ae939375222f757d61e7a094c3cba95a Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 5 Apr 2022 10:22:31 +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/VendorPlugin.php | 6 +++--- 7 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/Console/VendorExposeCommand.php b/src/Console/VendorExposeCommand.php index e55082d..01f6c95 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((string) $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((string) $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((string) 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((string) $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((string) $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((string) Util::joinPaths($path, '_config')) + || file_exists((string) Util::joinPaths($path, '_config.php')); } /** @@ -157,6 +157,6 @@ protected function isPathModule($path) */ protected function getProjectPath() { - return dirname(realpath(Factory::getComposerFile())); + return dirname((string) realpath((string) Factory::getComposerFile())); } } diff --git a/src/Library.php b/src/Library.php index 8b4a073..ba47992 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((string) $basePath); + $this->path = realpath((string) $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((string) $this->path, strlen((string) $this->basePath)), (string) 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((string) $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((string) $composer)) { return []; } $file = new JsonFile($composer); @@ -264,13 +264,13 @@ public function getExposedFolders() */ protected function validateFolder($exposeFolder) { - if (strstr($exposeFolder, '.')) { + if (strstr((string) $exposeFolder, '.')) { return false; } - if (strpos($exposeFolder, '/') === 0) { + if (strpos((string) $exposeFolder, '/') === 0) { return false; } - if (strpos($exposeFolder, '\\') === 0) { + if (strpos((string) $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((string) 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[/\\\\]#', (string) $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', (string) $resourcesDir)) { return $resourcesDir; } diff --git a/src/Methods/CopyMethod.php b/src/Methods/CopyMethod.php index 80896c6..c2b4a5c 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((string) $source)) { + return copy((string) $source, (string) $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((string) $file->getPathname(), (string) $targetPath); } } return $result; diff --git a/src/Methods/SymlinkMethod.php b/src/Methods/SymlinkMethod.php index 752b734..d2d83b0 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((string) $target, DIRECTORY_SEPARATOR); // Remove destination directory to ensure it is clean $this->filesystem->removeDirectory($target); // Ensure parent dir exist - $parent = dirname($target); + $parent = dirname((string) $target); $this->filesystem->ensureDirectoryExists($parent); // Ensure symlink exists diff --git a/src/Util.php b/src/Util.php index 6f47e8a..cd70900 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..44eae5e 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((string) $targetPath)) { $name = $file->getFilename(); $io->write("Writing {$name} to resources folder"); - copy($file->getPathname(), $targetPath); + copy((string) $file->getPathname(), (string) $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((string) $methodFilePath) && is_readable((string) $methodFilePath)) { + return trim((string) file_get_contents((string) $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((string) $methodFilePath, $key); } /** diff --git a/src/VendorPlugin.php b/src/VendorPlugin.php index 0fbd46c..8453ca5 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((string) realpath((string) 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((string) $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((string) $target); if ($this->filesystem->isDirEmpty($targetParent)) { $this->filesystem->removeDirectory($targetParent); }