laravel / pint Goto Github PK
View Code? Open in Web Editor NEWLaravel Pint is an opinionated PHP code style fixer for minimalists.
Home Page: https://laravel.com
License: MIT License
Laravel Pint is an opinionated PHP code style fixer for minimalists.
Home Page: https://laravel.com
License: MIT License
This happens on OSX (Intel). When pint removes unused variables in use statements of closures, it adds spaces before the commas. The validation then passes running pint --test
and no files need fixing. However, when I run the same command as part of my CI/CD pipeline on a CircleCI image, the files are flagged as breaking style convention.
original code
$img->text($this->getUnicodeCharFromIcon($icon), ($size / 2) + $hoffset, ($size / 2) + $voffset, function ($font) use ($icon, $img, $size, $iconSize, $large_color, $small_color) {
$font->file($this->getIconFontPath($icon));
$font->size($iconSize);
$font->color($small_color ?: $large_color);
$font->align('center');
$font->valign('center');
});
run fix on osx
./vendor/bin/pint
fixed code for reference
$small_color = data_get($params, 'color') ?: null;
$img->text($this->getUnicodeCharFromIcon($icon), ($size / 2) + $hoffset, ($size / 2) + $voffset, function ($font) use ($icon , $iconSize, $large_color, $small_color) {
$font->file($this->getIconFontPath($icon));
$font->size($iconSize);
$font->color($small_color ?: $large_color);
$font->align('center');
$font->valign('center');
});
run test on osx (will pass)
./vendor/bin/pint --test
run fix on linux machine (fails)
./vendor/bin/pint --test
Is it possible to separate the imports for traits? I couldn't find an option for this in php-cs-fixer but it may exist. Ideally, it would handle it similar to regular imports, one per Use
statement.
Since I frequently use multilingual applications and users often add new "sentences," then translation keys shouldn't be at the end in the xx.json
file. It would be good for pint
to check itself and possibly sort alphabetically by important key :)
Can we include tests/Feature to fix code styling?
It would be helpful to add to PINT to correct views Blade files, e.g. as Tlint does...
Pint could fix e.g. :
{{$variable}}
-> {{ $variable }}
or@if(!$variable)
-> @if (! $variable)
or@props([
'size'=>'normal',
'decorator' => false,
'title' =>''
])
->
@props ([
'size' => 'normal',
'decorator' => false,
'title' => '',
])
I believe it is an important feature to allow for passing list of files which should be formatted upon run instead of formatting all files. This is usable in case formatting is happening on pre-commit hook (for example in our case we have lint-staged
configured which is formatting staged PHP files before commit). I don't see this anywhere as a documented feature and the command input arguments also does not seem to allow for this.
Hey.
I see we have yet another wrapper of https://github.com/FriendsOfPHP/PHP-CS-Fixer/ .
I think it's totally fine and I can only encourage you to consider to also contribute back to the main project, especially with the generic parts. We already made a lot of elements customizable via interfaces, and we could make more customizations for sure.
Same time, if you decide to COPY-PASTE the source file from PHP CS Fixer repo, please remember to keep respect the original ownership and license.
The 3 rules were created by copy-pasting them from main repo and doing small adjustments, eg LaravelPhpdocOrderFixer is clone of PhpdocOrderFixer with slight adjustments to match your custom needs (most of logic, variable names, description etc is copy-pasted).
You are totally OK to do that as long as you keep the copyrights and license. Please, let's have respect to one another work.
(...) The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. (...)
I would like to use this in phpStorm similar to how php-cs-fixer can be used. Currently you can pass the configuration and file to fix on the command line for php-cs-fixer, so in phpStorm you can add it as an external tool and setup a hotkey to reformat the currently open file that you are editing.
I see the installation steps says to install locally in a project. Would it be unadvisable to install it as a global composer package (and why!) ?
Thank you,
Enum cases should not be reformatted even regardless of keyword name.
Enum class: app/Enum/SegmentCastMissingAs.php
namespace App\Enum;
enum SegmentCastMissingAs: string
{
case NULL = 'null';
case FALSE = 'false';
case TRUE = 'true';
case EMPTY_STRING = 'empty_string';
case ZERO = 'zero';
case IGNORE = 'ignore';
}
Pint change this style using Laravel preset:
-case NULL = 'null';
-case FALSE = 'false';
-case TRUE = 'true';
+case null = 'null';
+case false = 'false';
+case true = 'true';
case EMPTY_STRING = 'empty_string';
case ZERO = 'zero';
case IGNORE = 'ignore';
app/Enum/SegmentCastMissingAs.php
/vendor/bin/pint --preset laravel app/Enum/SegmentCastMissingAs.php
Hi,
I have a GitHub Org that has many Laravel apps. We currently have our own private coding standards repository which applies the same PHP CS Fixer rules across the organisation.
Is this same thing possible with Pint? We'd love to switch this over to use Pint but we don't really want to have to replicate the ruleset across all the applications, we'd rather this existed in a one centralised place across the whole organisation.
Cheers.
PHP-CS-Fixer has the option to configure the output format with --format
(https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/usage.rst#the-fix-command)
txt (default one), json, xml, checkstyle, junit and gitlab
currently i dont see an option to specify which output format to use which would be good to have in for example gitlab CI
I will happy if you want config only run it from specific folder.
Thanks.
Hi there,
I'm trying to run pint in a Docker container using a Makefile.
I keep getting this error :
Failed to write file "/tmp/e4d04fcf8f1486030e6781b8ac00870c", "file_get_con tents(phar:///var/www/vendor/laravel/pint/builds/pint/.env): Failed to open stream: phar error: ".env" is not a file in phar "/var/www/vendor/laravel/ pint/builds/pint"".
Here is my Makefile entry :
@echo "Executing Laravel Pint...";\
docker-compose -f docker-compose.yml exec -T workspace ./vendor/bin/pint ;\
echo "Job's done";\`
Cannot understand what pint is trying to do there.
When running it directly in Docker container everything works.
Do someone has any idea on how to solve this ?
Thanks in advance !
Hi,
Can be great to support rules options in the config file.
Options example: https://mlocati.github.io/php-cs-fixer-configurator/#version:3.8|fixer:new_with_braces
When I run the command ./vendor/bin/pint
I got an error
run in the terminal ./vendor/bin/pint
My pint.json
{
"preset": "laravel",
"rules": {
"@PSR2": true,
"@PSR12": true,
"group_import": true,
"array_indentation": true
}
}
I hope the package can render with UI soon!
Hi,
Is there exist task for GrumPHP to use Pint ?
Hello! It would be great to send a PR to PHP CS Fixer with the 3 Laravel rules.
Who's is brave enough to do this copy-paste job?
With php-cs-fixer it is possible to define an Indent and Line Ending besides to the rules:
->setIndent("\t")
->setLineEnding("\n")
->setRules([
//...
Dont blame me for that, but i want to have tabs instead of spaces for Indentation.
Vanilla php-cs-fixer
is doing that fine, but i dont see a way yet to set Indent and Line Ending in pint.json
.
./vendor/bin/pint --test is throwing this error
In Styles.php line 1012:
[Error]
Call to undefined function Termwind\ValueObjects\mb_strimwidth()
Exception trace:
at phar:///home/(project_path)/vendor/laravel/pint/builds/pint/vendor/nunomaduro/termwind/src/ValueObjects/Styles.php:1012
Termwind\ValueObjects\Styles::trimText() at phar:///home/(project_path)/vendor/laravel/pint/builds/pint/vendor/nunomaduro/termwind/src/ValueObjects/Styles.php:833
./vendor/bin/pint --init # Create pint.json when it is not exists
I'm using WSL2 with Sail. At this point, I'm trying to use PHPStorm to automatically run Pint on the current PHP file, which works correctly when I'm on a local path.
The issue comes when you open a project from the WSL UNC path (network share). For instance, my project is located at \\WSL$\UBUNTU\home\morfra\code\XXX
Here is the full error when running the following command:
cmd.exe /D /C call C:\Users\morfra\AppData\Roaming\Composer\vendor\bin\pint.bat \\wsl$\Ubuntu\home\morfra\code\XXX\app\Http\Middleware\RecordPageViewsToActivityLog.php
'\\wsl$\Ubuntu\home\morfra\code\XXX'
In Runner.php line 242:
Cannot write to file "\\WSL$\UBUNTU\home\morfra\code\refonte-intranet\app\Http\Middleware\RecordPageViewsToActivity
Log.php" as it is not writable.
That being said, Windows specifically says "UNC paths are not supported in CMD", but I've never had any issues with other scripts running like that.
Run the pint.bat command from Windows against a UNC path:
pint.bat "\\wsl$\Ubuntu\home\morfra\code\XXX\app\Http\Middleware\RecordPageViewsToActivityLog.php"
psr-12 preset is using incorrect if block structure with exclamation mark.
run the linter with an if statement with exclamation mark.
1-
- if (!empty($toRemove)) {
2-
+ if (! empty($toRemove)) {
also with docblocks:
1-
@param string $id
to
2-
@param string $id
and
1-
@param $id
to
2-
@param $id
I would say 1st examples supposed to be correct or am I missing something here ?
another issue is the removal of spaces before and after the . is half compliant with PSR-12
as it only recoomends to do it the same throughout the project -> and we used to use " . " and not "." what is it converted to now:
https://stackoverflow.com/a/66033448/1194797
https://xoops.gitbook.io/xoops-modules-cookbook/coding-standards/standards/styleguide#concatenation
- echo "Usage:" . PHP_EOL;
+ echo 'Usage:'.PHP_EOL;
thanks in advance.
pint is removing the null safe operator whilst fixing other issues. So before applying pint I have the following:
$result = $something?->else;
And after running pint
$result = $somethingelse;
After running some tests it seems to happen reliably when fixing other issues, for instance when reformatting braces, as shown below...
public function foo($something) {
$result = $something?->else;
}
public function foo($something)
{
$result = $somethingelse;
}
If the brace was in the correct place to start with:
public function foo($something)
{
$result = $something?->else;
}
Then running pint finds nothing to change or fix.
The Laravel Code Style has the following style throughout the source code:
laravel_phpdoc_alignment
After @param
should be two spaces then a type-hint
then another two spaces and then the $variable
/**
* Here is a description for what the function does
- * @param string $foo
+ * @param string $foo
* @return string
*/
public function bar($foo) {}
laravel_phpdoc_order
First should be the description
after that @param
then @return
and at last @throws
The default rule phpdoc_order
has a different order: description
, @param
, @throws
, @return
/**
* Here is a description for what the function does
* @param string $foo
* @param string $foo
- * @throws \Exception
* @return string
+ * @throws \Exception
*/
public function bar($foo) {}
laravel_phpdoc_seperation
Between each group of tags should be an empty line for seperation. @param
and @return
are always of the same group and are not to be seperated with an empty line.
The default behaviour for phpdoc_seperation
is also an empty line between @param
and @return
.
/**
* Here is a description for what the function does
+ *
* @param string $foo
* @param string $foo
- *
* @return string
+ *
* @throws \Exception
*/
public function bar($foo) {}
Since there are no default rules for PHP-CS-Fixer I created custom ones.
There are three Fixers for PHP-CS-Fixer which could be included here.
https://github.com/Jubeki/laravel-code-style/tree/main/src/Fixers
Do you have an interest in a PR?
Currently, Laravel Pint converts this:
<?php
if($something) {
// ...
}
to:
<?php
if ($something) {
// ...
}
It appears that it is the braces
rule that changes this, which is odd.
Is there any way to either disable this functionality, or better yet make Laravel Pint actually fix to the first one?
Could there be two sentences about why to use Pint over PHP-CS-Fixer?
I imagine the output might be more enjoyable and I imagine the installation being easier.
Is that correct?
But likely pint could get old as it comes bundled with a PHP-CS-Fixer, right?
Pint seems to be ignoring my pint.json
config when passing a file path.
Add a pint.json
with the following content:
{
"preset": "laravel"
}
./vendor/bin/pint app/example/file.php
- Laravel preset not applied
./vendor/bin/pint --preset=laravel app/example/file.php
- Laravel preset applied
This might be a bit farfetched but with all the mono-repo rage (and i.e. having a cross PHP version phar source in a larger project) one might want Pint to check everywhere but read the pint.json in the current working directory...
This is alot like how .gitignore works when placing it into a subdirectory and such. This would allow bigger repositories to have multiple rulesets, rules or even presets between directories and still "only" have the single pint command to fix it all ;)
Thanks!
When I'm working on a package, I composer symlink to the package's dev folder, so I can work in my laravel app on the package.
I have the VS Code extension installed and it ignores the vendor
folder, but I actually want it to format my package code, which is in the app's vendor
folder.
Perhaps an include
config that allows us to add specific folders that might otherwise be excluded?
Thanks!
I suggest creating pint installer so i can use it without the need to require it in any project.
Running using Laravel sail with Docker.
I get an error when running ./vendor/bin/pint. It prints out a couple of dots and checks, and then shows an exception:
./vendor/bin/pint -v
...........................................................................................
In HtmlRenderer.php line 32:
[Error]
Class "DOMDocument" not found
This is the full trace with pint -v
Exception trace:
at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/nunomaduro/termwind/src/HtmlRenderer.php:32
Termwind\HtmlRenderer->parse() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/nunomaduro/termwind/src/HtmlRenderer.php:24
Termwind\HtmlRenderer->render() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/nunomaduro/termwind/src/Functions.php:41
Termwind\render() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Output/SummaryOutput.php:62
App\Output\SummaryOutput->handle() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Actions/ElaborateSummary.php:47
App\Actions\ElaborateSummary->App\Actions\{closure}() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/support/helpers.php:302
tap() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Actions/ElaborateSummary.php:47
App\Actions\ElaborateSummary->execute() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Commands/DefaultCommand.php:55
App\Commands\DefaultCommand->handle() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Providers/CommandsServiceProvider.php:32
App\Providers\CommandsServiceProvider->App\Providers\{closure}() at n/a:n/a
call_user_func() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/container/Container.php:348
Illuminate\Container\Container->callMethodBinding() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/container/BoundMethod.php:90
Illuminate\Container\BoundMethod::callBoundMethod() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/container/BoundMethod.php:37
Illuminate\Container\BoundMethod::call() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/container/Container.php:651
Illuminate\Container\Container->call() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/console/Command.php:136
Illuminate\Console\Command->execute() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/symfony/console/Command/Command.php:291
Symfony\Component\Console\Command\Command->run() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/console/Command.php:121
Illuminate\Console\Command->run() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/symfony/console/Application.php:998
Symfony\Component\Console\Application->doRunCommand() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/symfony/console/Application.php:299
Symfony\Component\Console\Application->doRun() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/symfony/console/Application.php:171
Symfony\Component\Console\Application->run() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/illuminate/console/Application.php:102
Illuminate\Console\Application->run() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/laravel-zero/foundation/src/Illuminate/Foundation/Console/Kernel.php:129
Illuminate\Foundation\Console\Kernel->handle() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/vendor/laravel-zero/framework/src/Kernel.php:96
LaravelZero\Framework\Kernel->handle() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/app/Kernel.php:19
App\Kernel->handle() at phar:///home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint/pint:36
require() at /home/laura/PhpstormProjects/rm-notesync/vendor/laravel/pint/builds/pint:14
include() at /home/laura/PhpstormProjects/rm-notesync/vendor/bin/pint:120
All I did was run pint -v
, the project I'm working on is not open-source so I can't link that unfortunately.
it would be good to have a command such as
sail pint
or:
sail artisan pint
๐ there, congrats for the release @nunomaduro
Many code quality tools provide a phar version ( PHP-CS-Fixer, PHPMD, PHPStand, etc...)
Will pint also provide a .phar
file for each release ?
Also what about having a laravel/pint-github-action
, similar to https://github.com/OskarStark/php-cs-fixer-ga
Hi, currently I have many custom fixers, would be nice if there was a way to load them via the config file
Hi @nunomaduro
Any chance we someday get the PHPStorm IDE config available with rules to meet laravel
preset style?
Is there a way to highlight the code style issues while coding so I don't need to use terminal to update the code style.
Laravel make:migration
command create a migration file with braces on a new line. (e.g. https://github.com/laravel/laravel/blob/9.x/database/migrations/2019_12_14_000001_create_personal_access_tokens_table.php)
return new class extends Migration
{
// ....
};
Pint change this style even using Laravel preset:
-return new class extends Migration
-{
+return new class extends Migration {
// ...
};
artisan make:migration
/vendor/bin/pint --preset laravel
Hi @nunomaduro thanks for the great package! It was exactly what I was looking for!
Would you consider adding a baseline feature like in phpstan or phpmd for legacy projects ?
I get different results depending on where I start pint.
Running in sail with PHP 8.0.19: ./vendor/bin/pint --test
=> PASS
Running on MacOS with PHP 8.0.13: : ./vendor/bin/pint --test
=> FAIL
I assume, formatting has nothing to do with the PHP version. Can someone explain this to me?
Hello,
Sorry about the title was not really sure what to call it, I'm not sure if this is within the scope of the Pint project but through Laravel long lines are broken up into multiple lines example from FormRequest
return $factory->make(
$this->validationData(), $this->container->call([$this, 'rules']),
$this->messages(), $this->attributes()
)->stopOnFirstFailure($this->stopOnFirstFailure);
Would be nice if pint was able to detect and "fix" long lines like the above.
Running pint on a project with a very big seeder (10768002 bytes) produces a segfault.
Create a seeder with a collection of very big arrays. My case was with 160 insert statements, each with 500 array items, like the following.
DB::table('cities')->insert([
[
'id' => 1,
'nombre' => 'Guangzhou',
'pais_id' => '1',
],
...
[
'id' => 500,
'nombre' => 'Fotang',
'pais_id' => '1',
],
]);
I know we can exclude specific files with configuration rules, but it crashes... I'll expect the tool can check file size before checking a file and issue a warning, skipping the file and following with the next files. It's difficult to known what file was causing the segfault, because the tool doesn't provide an option to show files being analyzed or log progress to a file.
Based on issue #17, in my opinion being able to provide just a single file is a limitation that prevents usages with certain cases where we'd like to use this library to lint selected files, for example in case of pre-commit linting. Using the command to lint file by file would propably be a lot slower due to warm-up so being able to do
pint file1 file2 file3
or pint --file=file1 --file=file2
would propably be a much better idea.
I tried to manually exclude _ide_helper.php
, but it seems exclusion only works on entire folders. Adjacent to that, the
existing annotation-comments to skip php-cs-fixer are not considered.
Try to exclude at root _ide_helper.php
How to customize preset and use it?
As far as I know this is the only official Laravel code style preset. It would be nice if I could also use the rule preset with standard php-cs-fixer if I do not want to use the pint command. Pint makes things easy but since it is a layer on top of php-cs-fixer it does not have the full power and customization that php-cs-fixer allows.
For example, being able to reference @Laravel
like this in .php-cs-fixer.dist.php
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
return (new PhpCsFixer\Config())
->setUsingCache(true)
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@Laravel' => true,
])
->setFinder($finder);
Pint added spaces between | on explode function.
e.g from explode('|', $string)
to explode(' | ', $string)
So, this will give app errors. Is it expected?
i like to use cs2pr to get pr annotations for php-cs-fixer issues
here's a rough example github workflow :
name: Lint
jobs:
lint:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.0'
tools: cs2pr
- name: Run PHP Coding Standards Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --format=checkstyle | cs2pr
it would be great if pint supported github pr annotations
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.