Skip to content

Commit 7e814ca

Browse files
committed
Add exception for scandir failures
1 parent 89cef1c commit 7e814ca

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/Phug/Split/Command/Analyze.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Phug\Split\Command;
44

55
use Phug\Split;
6+
use Phug\Split\UnableToListDirectoryItems;
67
use SimpleCli\SimpleCli;
78
use Traversable;
89

@@ -106,8 +107,13 @@ protected function dumpPackagesTree(Split $cli, iterable $packages, int $level =
106107

107108
protected function mapDirectories(string $directory, callable $callback): iterable
108109
{
109-
/** @psalm-suppress RiskyTruthyFalsyComparison */
110-
foreach ((scandir($directory) ?: []) as $element) {
110+
$elements = scandir($directory);
111+
112+
if ($elements === false) {
113+
throw new UnableToListDirectoryItems($directory);
114+
}
115+
116+
foreach ($elements as $element) {
111117
if (substr($element, 0, 1) === '.') {
112118
continue;
113119
}

src/Phug/Split/Command/CommandBase.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ protected function gitEscape(string $value): string
5151
* @param array $options CLI git command options
5252
* @param string|null $redirect redirection suffix (like '2>&1')
5353
*
54+
* @psalm-param truthy-string|null $redirect
55+
*
5456
* @return string
5557
*
5658
* @psalm-suppress UndefinedThisPropertyFetch
@@ -61,7 +63,6 @@ protected function getGitCommand(string $command, array $options = [], ?string $
6163
$command .= ' --'.$name.'='.$this->gitEscape($value);
6264
}
6365

64-
/** @psalm-suppress RiskyTruthyFalsyComparison */
6566
return $this->gitProgram.' '.$command.($redirect ? ' '.$redirect : '');
6667
}
6768

@@ -72,6 +73,8 @@ protected function getGitCommand(string $command, array $options = [], ?string $
7273
* @param array $options CLI git command options
7374
* @param string|null $redirect redirection suffix (like '2>&1')
7475
*
76+
* @psalm-param truthy-string|null $redirect
77+
*
7578
* @return string
7679
*/
7780
protected function git(string $command, array $options = [], ?string $redirect = null): string
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Phug\Split;
4+
5+
use RuntimeException;
6+
use Throwable;
7+
8+
class UnableToListDirectoryItems extends RuntimeException
9+
{
10+
public function __construct(string $directory, int $code = 0, ?Throwable $previous = null)
11+
{
12+
parent::__construct("Unable to list items of the directory '$directory'", $code, $previous);
13+
}
14+
}

0 commit comments

Comments
 (0)