Skip to content

Commit

Permalink
Merge pull request #52 from creative-commoners/pulls/1/php81
Browse files Browse the repository at this point in the history
ENH PHP 8.1 compatibility
  • Loading branch information
GuySartorelli authored Apr 22, 2022
2 parents e74f4b8 + 0a8c886 commit f066154
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 41 deletions.
16 changes: 8 additions & 8 deletions src/Console/VendorExposeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -113,29 +113,29 @@ 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;
}
}

// Check all themes
$search = Util::joinPaths($basePath, 'themes', '*');
foreach (glob($search, GLOB_ONLYDIR) as $themePath) {
foreach (glob($search ?? '', GLOB_ONLYDIR) as $themePath) {
yield $themePath;
}
}
Expand All @@ -148,15 +148,15 @@ 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') ?? '');
}

/**
* @return string
*/
protected function getProjectPath()
{
return dirname(realpath(Factory::getComposerFile()));
return dirname(realpath(Factory::getComposerFile() ?? '') ?? '');
}
}
22 changes: 11 additions & 11 deletions src/Library.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 ?? '');
}

/**
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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) ?? '');
}

/**
Expand All @@ -293,7 +293,7 @@ public function publicPathExists()
*/
protected function installedIntoVendor()
{
return preg_match('#^vendor[/\\\\]#', $this->getRelativePath());
return preg_match('#^vendor[/\\\\]#', $this->getRelativePath() ?? '');
}

/**
Expand All @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Methods/CopyMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Methods/SymlinkMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/VendorExposeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <info>{$name}</info> to resources folder");
copy($file->getPathname(), $targetPath);
copy($file->getPathname() ?? '', $targetPath ?? '');
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -191,7 +191,7 @@ protected function getMethodKey()
protected function saveMethodKey($key)
{
$methodFilePath = $this->getMethodFilePath();
file_put_contents($methodFilePath, $key);
file_put_contents($methodFilePath ?? '', $key);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/VendorModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions src/VendorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function installRootPackage(Event $event)
*/
protected function getProjectPath()
{
return dirname(realpath(Factory::getComposerFile()));
return dirname(realpath(Factory::getComposerFile() ?? '') ?? '');
}

/**
Expand All @@ -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;
}

Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Methods/ChainedMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?? ''));
}
}
4 changes: 2 additions & 2 deletions tests/Methods/CopyMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions tests/Methods/SymlinkMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testSymlink()
}

// Parent folder is a real folder
$this->assertDirectoryExists(dirname($target));
$this->assertDirectoryExists(dirname($target ?? ''));
}

public function testSymlinkTrailingSlash()
Expand All @@ -86,7 +86,7 @@ public function testSymlinkTrailingSlash()
}

// Parent folder is a real folder
$this->assertDirectoryExists(dirname($target));
$this->assertDirectoryExists(dirname($target ?? ''));
}

public function testRecoversFromCopy()
Expand Down

0 comments on commit f066154

Please sign in to comment.