Skip to content

Commit 06c6059

Browse files
committed
Changes Api::extractMetaDataFromTreeInfo() to be more readable.
1 parent 1f81a3f commit 06c6059

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/Api.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ final public function getRecursiveMetadata($path, $recursive)
197197
$recursive
198198
);
199199

200+
$path = rtrim($path, '/') . '/';
201+
200202
$treeMetadata = $this->extractMetaDataFromTreeInfo($info[self::KEY_TREE], $path, $recursive);
201203

202204
return $this->normalizeTreeMetadata($treeMetadata);
@@ -255,29 +257,24 @@ private function authenticate()
255257
*/
256258
private function extractMetaDataFromTreeInfo(array $tree, $path, $recursive)
257259
{
258-
if (empty($path) === false) {
259-
$path = rtrim($path, '/') . '/';
260-
$metadata = array_filter($tree, function ($entry) use ($path, $recursive) {
261-
$match = false;
262-
263-
if (strpos($entry[self::KEY_PATH], $path) === 0) {
264-
if ($recursive === true) {
265-
$match = true;
266-
} else {
267-
$length = strlen($path);
268-
$match = (strpos($entry[self::KEY_PATH], '/', $length) === false);
269-
}
260+
$matchPath = substr($path, 0, -1);
261+
$length = strlen($matchPath) - 1;
262+
263+
$metadata = array_filter($tree, function ($entry) use ($matchPath, $recursive, $length) {
264+
$match = false;
265+
266+
$entryPath = $entry[self::KEY_PATH];
267+
268+
if ($matchPath === '' || strpos($entryPath, $matchPath) === 0) {
269+
if ($recursive === true) {
270+
$match = true;
271+
} else {
272+
$match = ($matchPath !== '' && strpos($entryPath, '/', $length) === false);
270273
}
274+
}
271275

272-
return $match;
273-
});
274-
} elseif ($recursive === false) {
275-
$metadata = array_filter($tree, function ($entry) use ($path) {
276-
return (strpos($entry[self::KEY_PATH], '/', strlen($path)) === false);
277-
});
278-
} else {
279-
$metadata = $tree;
280-
}
276+
return $match;
277+
});
281278

282279
return $metadata;
283280
}

0 commit comments

Comments
 (0)