Skip to content

[Filesystem] Make Filesystem methods static #60388

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

Open
shyim opened this issue May 9, 2025 · 2 comments
Open

[Filesystem] Make Filesystem methods static #60388

shyim opened this issue May 9, 2025 · 2 comments

Comments

@shyim
Copy link
Contributor

shyim commented May 9, 2025

Description

It would be nice if the methods inside the Filesystem class were all static, so you can call them without having to create an instance of that class.

Having to construct the class has no real benefit, it has no state inside the class

Example

// Instead of this
$fs = new Filesystem();
$fs->mkdir('foo');

// Or
(new Filesystem)->mkdir('foo');

// do directly this
Filesystem::mkdir('foo');
@nicolas-grekas
Copy link
Member

One of the reason why we have the Filesystem component is enabling mocking. Static calls would defeat that.

@GromNaN
Copy link
Member

GromNaN commented May 12, 2025

Beyond the initial intention, there are many places where the Filesystem class is used without dependency injection.

Even in our Kernel and FrameworkExtension classes

(new Filesystem())->remove(substr($legacyContainer, 0, -7));

$composerConfig = json_decode((new Filesystem())->readFile($composerFilePath), true, flags: \JSON_THROW_ON_ERROR);

Castor has the fs() function to access the Filesystem instance from it's global registry (its Container class)

The filesystem could be handled the same way we work with the Clock: have a default global instance that can be replaced if necessary with a mock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants