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

Add PHP support using php-cs-fixer #26

Merged
merged 6 commits into from
Jul 21, 2020
Merged

Add PHP support using php-cs-fixer #26

merged 6 commits into from
Jul 21, 2020

Conversation

jr-cologne
Copy link
Member

@jr-cologne jr-cologne commented Jul 9, 2020

This PR adds PHP support by making use of the formatting tool php-cs-fixer.

Unfortunately, it does not work yet as php-cs-fixer automatically uses the config from .php_cs.
Because of that, the wanted file in storage/code is not formatted as the specified files from the config file have precedence over the given file in the execution command.
At least, this is what I think is causing that the specified file is not formatted.

To fix this, I have tried to "docker-ignore" .php_cs. However, this does not work either.
In general, it looks like .dockerignore has no effect on which files are copied into the container.

Does anyone have an idea how to solve this or what is actually going on?

@jr-cologne jr-cologne added the enhancement New feature or request label Jul 9, 2020
@jr-cologne jr-cologne self-assigned this Jul 9, 2020
@jr-cologne jr-cologne requested a review from asccc July 9, 2020 12:25
Copy link
Member

@asccc asccc left a comment

Choose a reason for hiding this comment

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

the following suggestions are based on my recent pull-request (#27) - which should be merged before this pr, otherwise things will break 😃

we can pass rules as an argument to make php-cs-fixer ignore our php_cs file

.dockerignore Outdated Show resolved Hide resolved
app/CodeFormatter/PhpCodeFormatter.php Outdated Show resolved Hide resolved
app/CodeFormatter/PhpCodeFormatter.php Outdated Show resolved Hide resolved
app/CodeFormatter/PhpCodeFormatter.php Outdated Show resolved Hide resolved
app/CodeFormatter/PhpCodeFormatter.php Outdated Show resolved Hide resolved
@jr-cologne
Copy link
Member Author

Thank you for your help! 👍

@jr-cologne jr-cologne marked this pull request as ready for review July 11, 2020 18:28
@jr-cologne jr-cologne requested a review from asccc July 11, 2020 18:29
@jr-cologne
Copy link
Member Author

Hmm, I'm still getting an error with regards to the php-cs-fixer config/cache files:

stderr: Loaded config default. Using cache file ".php_cs.cache".

Any idea why this is?

asccc
asccc previously requested changes Jul 12, 2020
Copy link
Member

@asccc asccc left a comment

Choose a reason for hiding this comment

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

The formatter complains, because php-cs-fixer writes stuff to stderr,
I made some suggestions to fix this, but i have not tested them.

The fact that php-cs-fixer used its default config is good, because this means that it did not load our .php_cs file.

app/CodeFormatter/PhpCodeFormatter.php Outdated Show resolved Hide resolved
@asccc
Copy link
Member

asccc commented Jul 12, 2020

Forget what i said. php-cs-fixer does not support stdin/stdout fixing (using pipes).

It supports file-content from stdin, but does not write the result to stdout ... it only supports "diff" ...
We could apply the diff to the content but that would make things unnecessarily complicated.

Instead we could try the php formatter from prettier.
The prettier php-formater is not as sophisticated as php-cs-fixer, but probably good enough™. And to be honest, in most cases, we just want to fix indentations in user provided scripts and not enforce PSR standards to it, because this could lead to situations where users do not recognize their own code anymore 😆

@jr-cologne
Copy link
Member Author

Yeah, ok. I kind of suspected that already.

For the future, it might be a good idea to support both ways of input/output - to a temp file as well as stdin/stdout using pipes.

In #11, @wookiefriseur mentioned that Prettier plugins are way to slow which is why we decided not to use Prettier for formatting PHP.
Of course, we could still try it again.

@asccc
Copy link
Member

asccc commented Jul 21, 2020

Since #39 was merged (🎉) we could take look at prettier-plugin-php again.
Maybe the performance is reasonable now?

@jr-cologne
Copy link
Member Author

Yep, I am going to work on that pretty soon.
After merging this PR, I will finally release and deploy a new version to production.

This change ensures the original code-body is returned if code formatting fails.
@jr-cologne jr-cologne requested a review from asccc July 21, 2020 18:53
@jr-cologne
Copy link
Member Author

I think we are good to go. 😃

Prettier is significantly faster than before.
In manual testing, disabling prettierd raised the response time from around 100ms to 700ms.

@asccc
Copy link
Member

asccc commented Jul 21, 2020

Nice, this sounds very promising!

@jr-cologne jr-cologne merged commit 44fdf6b into master Jul 21, 2020
@jr-cologne jr-cologne deleted the php-support branch July 21, 2020 19:08
@jr-cologne
Copy link
Member Author

Oops, merged it with the wrong title/description as we are not using php-cs-fixer anymore. 😄

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

Successfully merging this pull request may close these issues.

2 participants