lkrms / pretty-php Goto Github PK
View Code? Open in Web Editor NEWThe opinionated PHP code formatter
License: MIT License
The opinionated PHP code formatter
License: MIT License
For example:
$abc = a($b,
$c);
$d = a($b, $c);
For example:
switch ($operator) {
default:
case '=':
case '==': return $retrieved == $value;
case '!=':
case '<>': return $retrieved != $value;
case '<': return $retrieved < $value;
case '>': return $retrieved > $value;
case '<=': return $retrieved <= $value;
case '>=': return $retrieved >= $value;
case '===': return $retrieved === $value;
case '!==': return $retrieved !== $value;
case '<=>': return $retrieved <=> $value;
}
,
, ;
)--operators-first
/--operators-last
PrettyPHP's default formatting is largely PSR-12 compliant, but there are some differences to address.
Parts of PSR-12, e.g. PascalCase
class names, fall outside the scope of a formatter and won't be implemented, although exceptions are sometimes made (use
import statements are already sorted by default, for example).
PreserveOneLineStatements
must not be enabled)SpaceDeclarations
and SortImports
mandatory):
<?php
namespace
currently)declare
statementsnamespace
declarationuse
statementsuse
statementsuse
statementsNoMixedLists
mandatory):
implements
and extends
interfaces
implements
or extends
, not just between first two interfacesclass
with an interface list that wrapsfunction
declarationsuse
declarationsfunction
callsfor
loopsdeclare
at line 1 of files also containing markup: <?php declare(strict_types=1) ?>
catch
pretty-php adds 2 spaces before the comment. expected 1.
Not sure if this is intentional, but I've never seen this style before.
<?php
class Example
{
public int $test = 0; // This is a test.
public function example()
{
$this->test = 1; // This is a test.
}
}
<?php
exit();
die();
Incorrectly changes to:
<?php
exit ();
die ();
PrettyPHP v0.4.16 produces this:
<?php
class A
{
private $b;
protected function c()
{
return $this->b
?? ($this->b = implode(
':', ['a',
'b',
'c',
'd',
'e']
));
}
}
Instead of this:
<?php
class A
{
private $b;
protected function c()
{
return $this->b
?? ($this->b = implode(
':', ['a',
'b',
'c',
'd',
'e']
));
}
}
The last colon here should not be indented:
<?php
$threshold =
$this->UncertaintyThreshold === null
? null
: (is_array($this->UncertaintyThreshold)
? $this->UncertaintyThreshold[$algorithm] ?? null
: $this->UncertaintyThreshold);
SortImports
AlignComments
For example:
/* line 1
line 2 */
e.g.
$string = "Value
\twith
\ttabs.";
Should be:
$string = "Value
with
tabs.";
TokenTypeTest
out to TokenTypeIndexTest
Current output:
$a = new class implements
P,
Q {
};
Expected output:
$a = new class implements
P,
Q
{
};
Required for PSR-12 compliance (see #4).
e.g. this is how ternary operators are formatted:
<?php
return
$value
? $value->id
: null;
vs. the coalesce operator:
<?php
return
$value
?? null;
If this establishes a conflict with other options, compliance should take precedence. Runtime user feedback need not be given.
Is there a way to turn this:
if (CODE) {
// code
} else
if (CODE) {
// code
} else
if (CODE) {
// code
}
into this:
if (CODE) {
// code
} else if (CODE) {
// code
} else if (CODE) {
// code
}
May require detection of terminating statements / // No break
separators.
Hi there, thanks for pretty-php
I was running it (downloaded .phar version) over a project and ran into the following when it was trying to process a particular file:
Call to a member function declarationParts() on bool in phar:///usr/local/bin/pretty-php/src/Rule/DeclarationSpacing.php:218
Unfortunately I can't show you the code, but I can give you a close approximation of the first few lines of the file (no blank lines, no comments immediately after opening php tag):
<?php
date_default_timezone_set("Relevant/Timezone");
global $bad_practice;
define('SOME_VAR', 42);
/**
* comment
*/
class SomeClass {
...
I did some messing around and found that I was able to work around this by inserting some blank lines at the start of the file, thusly:
<?php
date_default_timezone_set("Relevant/Timezone");
...
Hope this helps someone! :)
Before:
<?php
enum ExampleEnum {
/**
* Note.
*/
case Foo;
# Note.
case Bar;
// Note.
case Qux;
}
After:
<?php
enum ExampleEnum
{
/** Note. */
case Foo;
// Note.
case Bar;
// Note.
case Qux;
}
For example, the hanging indentation here is unnecessary:
<?php
final class ErrorHandler
{
private const FATAL_ERRORS =
E_ERROR
| E_PARSE
| E_CORE_ERROR
| E_CORE_WARNING
| E_COMPILE_ERROR
| E_COMPILE_WARNING;
}
A rough starting point:
.OH.
is applied if:
- the open bracket of the block is not followed by a newline, AND
- either:
the block contains
,
- or;
- delimited items, ORthe block forms part of a structure that continues, e.g.
if ($block) { // continuation }1. Standard indentation is sufficient [ ___, ___, ___, ___ ] 2. One level of hanging indentation is required [ ___, ___ .hh.___, ___, ___, ___ ] 3. One level of hanging indentation is sufficient [___, ___, .hh.___, ___] 4. Two levels of hanging indentation are required [___, ___ .hh..OH.___, ___, .hh.___, ___] 5a. Two levels of hanging indentation are required per level of nesting [___, [___, .hh..OH..hh.___], .hh.___,[___, ___ .hh..OH..hh..OH.___, .hh..OH..hh.___]] 5b. [[[___ .hh..OH..hh..OH..hh..OH.___, .hh..OH..hh..OH..hh.___], .hh..OH..hh.___], .hh.___]
e.g.
<?php
foo($bar, $qux, $quux
? null
: $quuux);
// vs.
foo(
$bar,
$qux,
$quux
? null
: $quuux
);
Also consider:
As per PSR-12, the default line length "SHOULD NOT" exceed 80 characters and "MUST" have a soft limit of 120 characters.
e.g.
<?php
$string = "\rText after carriage return";
$string = "\rMultiline text after carriage return
";
Becomes:
<?php
$string = "\rText after carriage return";
$string = '
Multiline text after carriage return
';
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.