Skip to content

Commit

Permalink
Merge pull request #1749 from tinect/style/InMemoryCode
Browse files Browse the repository at this point in the history
style: [InMemory] update method usages to PHP 8.0
  • Loading branch information
frankdejonge authored Feb 4, 2024
2 parents 57c8534 + 0f7fa06 commit 131782c
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/InMemory/InMemoryFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@

use function array_keys;
use function rtrim;
use function strpos;

class InMemoryFilesystemAdapter implements FilesystemAdapter
{
const DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST = '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST';
public const DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST = '______DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST';

/**
* @var InMemoryFile[]
Expand Down Expand Up @@ -85,14 +84,14 @@ public function delete(string $path): void
unset($this->files[$this->preparePath($path)]);
}

public function deleteDirectory(string $prefix): void
public function deleteDirectory(string $path): void
{
$prefix = $this->preparePath($prefix);
$prefix = rtrim($prefix, '/') . '/';
$path = $this->preparePath($path);
$path = rtrim($path, '/') . '/';

foreach (array_keys($this->files) as $path) {
if (strpos($path, $prefix) === 0) {
unset($this->files[$path]);
foreach (array_keys($this->files) as $filePath) {
if (str_starts_with($filePath, $path)) {
unset($this->files[$filePath]);
}
}
}
Expand All @@ -105,11 +104,11 @@ public function createDirectory(string $path, Config $config): void

public function directoryExists(string $path): bool
{
$prefix = $this->preparePath($path);
$prefix = rtrim($prefix, '/') . '/';
$path = $this->preparePath($path);
$path = rtrim($path, '/') . '/';

foreach (array_keys($this->files) as $path) {
if (strpos($path, $prefix) === 0) {
foreach (array_keys($this->files) as $filePath) {
if (str_starts_with($filePath, $path)) {
return true;
}
}
Expand Down Expand Up @@ -184,9 +183,9 @@ public function listContents(string $path, bool $deep): iterable
$prefixLength = strlen($prefix);
$listedDirectories = [];

foreach ($this->files as $path => $file) {
if (substr($path, 0, $prefixLength) === $prefix) {
$subPath = substr($path, $prefixLength);
foreach ($this->files as $filePath => $file) {
if (str_starts_with($filePath, $prefix)) {
$subPath = substr($filePath, $prefixLength);
$dirname = dirname($subPath);

if ($dirname !== '.') {
Expand All @@ -200,20 +199,20 @@ public function listContents(string $path, bool $deep): iterable

$dirPath .= $part . '/';

if ( ! in_array($dirPath, $listedDirectories)) {
if ( ! in_array($dirPath, $listedDirectories, true)) {
$listedDirectories[] = $dirPath;
yield new DirectoryAttributes(trim($prefix . $dirPath, '/'));
}
}
}

$dummyFilename = self::DUMMY_FILE_FOR_FORCED_LISTING_IN_FLYSYSTEM_TEST;
if (substr($path, -strlen($dummyFilename)) === $dummyFilename) {
if (str_ends_with($filePath, $dummyFilename)) {
continue;
}

if ($deep === true || strpos($subPath, '/') === false) {
yield new FileAttributes(ltrim($path, '/'), $file->fileSize(), $file->visibility(), $file->lastModified(), $file->mimeType());
if ($deep === true || ! str_contains($subPath, '/')) {
yield new FileAttributes(ltrim($filePath, '/'), $file->fileSize(), $file->visibility(), $file->lastModified(), $file->mimeType());
}
}
}
Expand Down

0 comments on commit 131782c

Please sign in to comment.