mmoreram / php-formatter Goto Github PK
View Code? Open in Web Editor NEWPHP Formatter is a PHP developer friendly set of tools
License: MIT License
PHP Formatter is a PHP developer friendly set of tools
License: MIT License
When sorting use clauses in PHP files under a Windows system (I used Windows 7, with PHP 7 RC5), the sorting makes the file invalidate a PSR-2 rule, as the line endings appended are the default from Windows (\r\n), instead of the ones from UNIX (\n).
Please see the attached image to apreciate that the endings have only been replaced in use clauses.
Hi,
What is the project status?
1 - It is abandoned?
2 - It has support for PHP 7.4?
I have been noticing this issue often and have been meaning to attempt to fix it. I haven't had time and I keep forgetting so I am creating this issue as a kind of reminder. Also hoping that someone will know exactly why this is happening and be able to submit a fix faster than me.
It looks like when sorting use-statements in fairly complex groups and two groups consecutively are not being used there are some additional spaces added.
Example pulled from my test classes.
use Project\Core\AClass;
use Project\Core\BClass;
use Project\Core\CClass;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
With the following configuration:
use-sort:
group:
- Project\Core
- Project\Integration
- [Symfony, Sensio, Doctrine]
- _main
group-type: each
group-skip-empty: true
sort-type: alph
sort-direction: asc
Please note that group-skip-empty
is enabled also.
Expecting output:
use Project\Core\AClass;
use Project\Core\BClass;
use Project\Core\CClass;
use PHPUnit_Framework_MockObject_MockObject as MockObject;
The use:sort
command appears to have a bug where a class that has no imports but uses a trait from the same namespace it will treat the trait usage as if it was a file level import. This is causing issues in our builds which will fail the code on bad smell.
The replication is pretty simple. Have one class and a trait in the same namespace, make the class use the trait without import (as you should). In the examples below I added a method also to demonstrate that it also messes with the formatting of methods.
The command:
vendor/bin/php-formatter formatter:use:sort src --ansi
File structure:
src/MyTrait.php
src/MyClass.php
The class:
<?php
namespace SomeNamespace;
class MyClass
{
use MyTrait;
public function something()
{
return 1;
}
}
Now run the command above on the src
directory and the output will look something like this:
<?php
namespace SomeNamespace;
class MyClass
{
use MyTrait;
public function something()
{
return 1;
}
}
Now obviously the temporary fix for this is to import the trait. Unless you are using an auto code formatter in which it will remove the useless import and then you are back where you started. Have you come across this issue? Personally I have never written a trait in the same directory as something implementing it one of my co-workers has and now this problem falls on me.
I am having issues when running this on my docker box with the latest changes.
I find it strange that this gets past the build, but does the build ever test through the Console\Application
class? This might want to be something we look in too if not?
Fatal error: Uncaught TypeError: ini_set() expects parameter 2 to be string, boolean given in /src/PHPFormatter/Console/Application.php on line 35
TypeError: ini_set() expects parameter 2 to be string, boolean given in /src/PHPFormatter/Console/Application.php on line 35
Call Stack:
0.0007 351696 1. {main}() /bin/php-formatter:0
0.0180 863312 2. Mmoreram\PHPFormatter\Console\Application->__construct() /bin/php-formatter:25
0.0180 863312 3. ini_set() /src/PHPFormatter/Console/Application.php:35
The culprit looks to be here as described in the above stack and in the documentation:
https://github.com/mmoreram/php-formatter/blob/v1.3.1/src/PHPFormatter/Console/Application.php#L34-L37
And looks like PHP documentation has been updated for PHP 7.0.
string ini_set(string $varname , string $newvalue)
http://php.net/manual/en/function.ini-set.php
$ php -v
PHP 7.0.12 (cli) (built: Nov 8 2016 23:49:58) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Xdebug v2.5.0rc1, Copyright (c) 2002-2016, by Derick Rethans
How do i format php code stored in string?
I would like to suggest some new features for the sorter:
_annotations
which groups annotations (is that possible with the current implementation/in general?)use-sort:
group:
- Symfony
- _main
- App
- ['Sensio\Bundle\FrameworkExtraBundle\Configuration', 'JMS\DiExtraBundle\Annotation']
group-type: each
sort-type: alph
sort-direction: asc
Wdyt @mmoreram ?
Is it possible to add excluded or ignored directories? It would be great to be able to prevent it from trying to sort use statements in my vendor
directory or something.
For each group specified there is an additional empty line. I don’t think that’s intended!?
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.