Skip to content

Separate file/directory/exists assertions#291

Merged
shadowhand merged 1 commit intowebmozarts:masterfrom
shadowhand:patch-1
Oct 16, 2024
Merged

Separate file/directory/exists assertions#291
shadowhand merged 1 commit intowebmozarts:masterfrom
shadowhand:patch-1

Conversation

@shadowhand
Copy link
Copy Markdown
Collaborator

There is no need to use file_exists before using is_file and is_dir, this makes for confusing errors. For instance, if you call Assert::directory('invalid/path') then the reported error will be:

The file "invalid/path" does not exist.

Additionally, the messages for these assertions were either technically or grammatically incorrect.

if (!\file_exists($value)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'The file %s does not exist.',
$message ?: 'The path %s does not exist.',
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically incorrect, since file_exists is equivalent to:

return is_file($path) || is_dir($path);

*/
public static function file($value, $message = '')
{
static::fileExists($value, $message);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pointless check.

*/
public static function directory($value, $message = '')
{
static::fileExists($value, $message);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pointless check. If the directory does not exist, the next assertion will never run. If the directory does exist, then it duplicates work.

if (!\is_dir($value)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'The path %s is no directory.',
$message ?: 'The path %s is not a directory.',
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this message consistent with Assert::file().

*/
public static function fileExists($value, $message = '')
{
static::string($value);
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see other assertions doing this, so I removed it.

There is no need to use `file_exists` before using `is_file` and `is_dir`, this makes for confusing errors.
@shadowhand shadowhand merged commit 781144a into webmozarts:master Oct 16, 2024
@shadowhand shadowhand deleted the patch-1 branch October 16, 2024 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant