Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

[CodingStandard] sniff ideas #59

Closed
13 tasks done
TomasVotruba opened this issue Jan 13, 2017 · 10 comments
Closed
13 tasks done

[CodingStandard] sniff ideas #59

TomasVotruba opened this issue Jan 13, 2017 · 10 comments
Assignees

Comments

@TomasVotruba
Copy link
Member

TomasVotruba commented Jan 13, 2017

AST Refactors

  • Moved to Rector

  • sniffs teaching defensive programing - https://ocramius.github.io/extremely-defensive-php

  • refactor named service to typed service in Symfony

    • get container map

    • replace string by type

    • or by interface that implements

    • configurable

      # name: type
      doctrine.entity_manager: Doctrine\ORM\EntityManagerInterface   

Finished Ideas

Outsourced

@TomasVotruba
Copy link
Member Author

Might try fixer as well

@SpacePossum
Copy link

SpacePossum commented Jun 29, 2017

uninitialized array sniff php

I think you check that one off now :)

no fluent interface, return $this;

out of curiosity, why do you not like this?

php unresolved conflict sniff

looking at the sample, a php -l would've prevented to merge the conflict, but granted if the conflict is within non-meaningful lines (comments etc.) or strings or any non-php-source file this would not have helped.

@TomasVotruba
Copy link
Member Author

I think you check that one off now :)

You're right, done!

out of curiosity, why do you not like this?

It's not about not-liking fluent interfaces. They have their purpose and I'd use them in some places.
I see them in many PHP projects I mentor. I ask "Why?" The most common and to me surprising answer is : "Well, PHPStorm generates them, it must be good". I did too with same ignorace, until I read post about interfaces by @Ocramius

looking at the sample, a php -l would've prevented to merge the conflict, but granted if the conflict is within non-meaningful lines (comments etc.) or strings or any non-php-source file this would not have helped.

Yep, linter would do that. But it would require and extra tool to use in every place. Lazy people (me) won't use it. How would you put that into fixer?

@SpacePossum
Copy link

SpacePossum commented Jun 30, 2017

Thanks for the link to the blog of Marco, hadn't read this post before. I always like to read his blog even if I don't agree on all of it. .

On the linting; if you've the PHP-CS-Fixer available you can use that one as linter as well. The fixer selects a linter (typically, you can use another when playing around with the configuration) based on the PHP executable found, it prefers one with tokenizing support otherwise it will fallback on the regular PHP executable using -l.
You can use this by fixing the code with dry-run for example;

$ php-cs-fixer fix test.php -vvv --dry-run
Loaded config default.
Using cache file ".php_cs.cache".
I
Legend: ?-unknown, I-invalid file syntax, file ignored, S-Skipped, .-no changes, F-fixed, E-error

Fixed all files in 0.004 seconds, 4.000 MB memory used

Files that were not fixed due to errors reported during linting before fixing:
   1) /home/possum/playground/test.php

                                                                          
        [PhpCsFixer\Linter\LintingException]                              
        PHP Parse error: syntax error, unexpected end of file on line 3.  

$ echo $?
4
                                                                          

The exit-code 4 is only used when using dry-run and can be used in scripts to detect lint errors.

If you don't want to fix the code using the fixer for linting is a bit of an overkill of course, the more easy way would be to use PHP itself directly.

$ php -l test.php
PHP Parse error:  syntax error, unexpected end of file in test.php on line 3
Errors parsing test.php

$ echo $?
255

(PS:

strict types position <?php declare...

keep on eye on PHP-CS-Fixer/PHP-CS-Fixer#2891 ;) )

@TomasVotruba
Copy link
Member Author

TomasVotruba commented Jul 5, 2017

Thanks for sharing link to "strict_types" location! 👍

Where exactly is that "linter" located in PHP-CS-Fixer? I need fixer class as I don't use CLI, but mostly only Fixer classes.

@SpacePossum
Copy link

The linter is not used in a fixer so that might make it more tricky to reuse in your case.
You can follow the path down to here;
https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/2.3/src/Runner/Runner.php#L226
there the linter is used to check a string input (read from a file) to check if it is in valid PHP syntax.
(Little note, linting code on PHP5.x is much slower than on PHP7, unfortunately there is not something that can be helped)
The linter is derived from passed configuration, but typically it will use the PHP executable using php -l.

@TomasVotruba
Copy link
Member Author

I think I'll pass the linter now. I'm much more into more specific checkers. Thanks for clues anyway, they might be useful for me in the future.


I came across this again and wonder... do you know any fixer doing this?

Before

$elements[] = ['file' => $path, 'label' => $name];

After

$elements[] = [
    'file' => $path,
    'label' => $name
];

To make list arrays items exactly 1 per line.

@SpacePossum
Copy link

it was proposed here PHP-CS-Fixer/PHP-CS-Fixer#1217
(I think its the same?), however no follow up was done on it

@TomasVotruba
Copy link
Member Author

Thanks, I'll check it

@TomasVotruba
Copy link
Member Author

Rest of more complex checkers moved to: https://github.com/TomasVotruba/Rector

@deprecated-packages deprecated-packages locked as resolved and limited conversation to collaborators Oct 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants