Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

D10-9: Safer PHP array destructuring. #18

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/StreamWrapper/Foxml.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ protected function getLocalPath($uri = NULL) {
$uri = $this->uri;
}
$target = $this->getTarget($uri);
[$subtype, $target_actual] = explode('/', $target, 2);
$exploded = explode('/', $target, 2);
if (count($exploded) === 1) {
// XXX: `foxml://object` and `foxml://datastream` on their own do not
// really point anywhere.
return FALSE;
}
[$subtype, $target_actual] = $exploded;
assert(in_array($subtype, ['object', 'datastream']), 'Valid URI.');

try {
Expand Down
26 changes: 14 additions & 12 deletions src/Utility/Fedora3/ArchivalObjectLowLevelAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,25 @@ public function getIterator() : \Traversable {
// Recurse through the directory.
$files = new \RecursiveDirectoryIterator($this->basePath, $flags);

// Pre-filter deletable files away.
// Adapted from: https://github.com/discoverygarden/akubra_adapter/blob/38000f45a25cf99c5f34a53cf93193e6fdff88b9/src/Utility/Fedora3/ObjectLowLevelAdapter.php#L35-L42
$non_deletable_files = new \RecursiveCallbackFilterIterator(
$files,
function (\SplFileInfo $file) {
return $file->isDir() || ($file->isFile() && $file->isReadable() &&
!$file->isWritable() && !$file->getPathInfo()->isWritable());
}
);

// A filter could be added here if necessary.
if ($this->pattern) {
$filter = new \RecursiveCallbackFilterIterator(
$files,
$non_deletable_files,
[$this, 'filterCallback']
);
}
else {
$filter = $files;
$filter = $non_deletable_files;
}

// Get an iterator of our iterator...
Expand Down Expand Up @@ -87,28 +97,20 @@ public function getIteratorType() : int {
*
* @param \SplFileInfo $current
* The item being considered.
* @param mixed $key
* The key for the item being considered.
* @param \Iterator $iterator
* The iterator in which this filter callback is being used.
*
* @return bool
* TRUE if the given item should be considered; otherwise, FALSE.
*
* @see https://github.com/discoverygarden/migrate_directory/blob/87d47cd69f037f5d239af0c8124c3ed433146413/src/Plugin/migrate/source/MigrateDirectory.php#L49-L62
*/
protected function filterCallback(\SplFileInfo $current, $key, \Iterator $iterator) : bool {
// Get the current item's name.
/** @var \SplFileInfo $current */
$filename = $current->getFilename();

protected function filterCallback(\SplFileInfo $current) : bool {
if ($current->isDir()) {
// Always descend into directories.
return TRUE;
}

// Match the filename against the pattern.
return preg_match($this->pattern, $filename) === 1;
return preg_match($this->pattern, $current->getFilename()) === 1;
}

/**
Expand Down