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

Use exception for system errors #3739

Closed

Conversation

yguedidi
Copy link
Contributor

@yguedidi yguedidi commented May 6, 2023

I believe this makes the API for some services cleaner, and it makes sense to use exceptions for errors and catch them at the right high level, not too soon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Introducing this chain file processor is necessary because the processFile() function became similar in both ApplicationFileProcessor and WorkerRunner, to make PHPStan happy, also because of complexity compliance

Comment on lines -51 to -53
/**
* @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed because in the interface already

Comment on lines -129 to -131
/**
* @return string[]
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed because in the interface already

Comment on lines -38 to -40
/**
* @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed because in the interface already

Comment on lines -92 to -94
/**
* @return string[]
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed because in the interface already

@yguedidi yguedidi force-pushed the use-exception-for-system-errors branch from 195e1e3 to 599c0e6 Compare May 6, 2023 13:46
$this->invalidateFile($file);
} elseif (! $configuration->isDryRun()) {
if ($fileDiff instanceof FileDiff) {
array_unshift($fileDiffs, $fileDiff);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

using unshift here to have current expected e2e test output green.
I'd suggest to move to fileDiffs[] = fileDiff; and adapting the expected output files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but in another PR

$fileDiff = $this->chainFileProcessor->process($file, $configuration);

if ($fileDiff instanceof FileDiff) {
array_unshift($systemErrorsAndFileDiffs[Bridge::FILE_DIFFS], $fileDiff);
Copy link
Contributor Author

Choose a reason for hiding this comment

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


final class ParsingException extends Exception
{
public function __construct(public readonly SystemError $systemError)
Copy link
Contributor Author

@yguedidi yguedidi May 6, 2023

Choose a reason for hiding this comment

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

I'll make this property private in next rebase+push after dependency PRs are merged.
It's a leftover from a previous test

@yguedidi
Copy link
Contributor Author

yguedidi commented May 6, 2023

@samsonasik this should be ready for a first look if you want! :)
will mark it as officially ready for review after the dependency PRs are merged

@yguedidi yguedidi force-pushed the use-exception-for-system-errors branch from 599c0e6 to bb3e24c Compare May 6, 2023 15:10
@yguedidi yguedidi marked this pull request as ready for review May 6, 2023 15:12
@TomasVotruba
Copy link
Member

This PR is changing too much of architecture we keep up with PHPStand and ECS. It makes it easier to maintain and extend along with other projects :)

That's why I want to keep those untouched. Thanks for understanding

@yguedidi
Copy link
Contributor Author

yguedidi commented May 6, 2023

@TomasVotruba to be honest I'm trying to understand but not really 😅

I can understand the need for synchronicity with your other projects, but I can open PRs there to align to the better architecture.
I understand too the resistance to internal changes for safety.
But what if previous architecture is not that simple? or blocking potential new feature? we stick to it without reworking it?

What this PR is doing is not complex, it simplify the API of FileProcessorInterface from

    /**
     * @return array{system_errors: SystemError[], file_diffs: FileDiff[]}
     */
    public function process(File $file, Configuration $configuration): array;

to

    /**
     * @throws ParsingException
     */
    public function process(File $file, Configuration $configuration): ?FileDiff;

To me it's not a file processor responsibility to manage the list of system errors and all file diffs.
We should expect from it to give us a FileDiff or not, that's it, like this PR is doing.
And let the potential errors be managed at a higher level thanks to exceptions.
Isn't it a improvement that follows best practices and good architecture?

BTW, this will also be helpful for the proposed idea in rectorphp/rector#7915 (if you can have a look it would be great!)
And that one will have a huge user impact IMO 🙂

@samsonasik
Copy link
Member

@yguedidi that will break rector extension projects that consume the interface.

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

Successfully merging this pull request may close these issues.

3 participants