GithubHelp home page GithubHelp logo

lkrms / pretty-php Goto Github PK

View Code? Open in Web Editor NEW
31.0 31.0 0.0 8.83 MB

The opinionated PHP code formatter

License: MIT License

PHP 98.31% Shell 1.48% Latte 0.21%
deterministic formatter opinionated php

pretty-php's People

Contributors

lkrms avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pretty-php's Issues

Align one-line switch cases?

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;
}

Review `AlignData`

  • Improve documentation to clarify why tokens are eligible or ineligible for alignment
  • Optimize for performance
  • Test "relaxed" mode with a view to surfacing more specific controls for presets, or (preferably) improving default behaviour so it can be removed

Add support for formatting-related requirements of PSR-12

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).

  • LF line endings
  • Indent of 4 spaces
  • Line length "SHOULD NOT" exceed 80 characters and "MUST" have a soft limit of 120 characters
  • One statement per line (PreserveOneLineStatements must not be enabled)
  • Blank lines after "header blocks" (SpaceDeclarations and SortImports mandatory):
    • <?php
    • file-level docblock (blank line only applied before namespace currently)
    • declare statements
    • namespace declaration
    • class use statements
    • function use statements
    • constant use statements
  • Allow breaking over multiple lines if every item is on its own line (NoMixedLists mandatory):
    • implements and extends interfaces
      • Trigger if a newline appears after implements or extends, not just between first two interfaces
      • Add a newline before the opening brace of an anonymous class with an interface list that wraps
    • Parameters in function declarations
    • Variables in anonymous function use declarations
    • Arguments in function calls
    • Statements in for loops
  • One-line declare at line 1 of files also containing markup: <?php declare(strict_types=1) ?>
    • Suppress newline before close tag even if present in source
  • Force newlines before and after control structure expressions split over multiple lines
  • Add whitespace between exception types in catch
  • Enforce newline before first method in multiline method chains

Double spaces before comments on the end of the code line

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.
    }
}

exit

<?php

exit();
die();

Incorrectly changes to:

<?php

exit ();
die ();

Fix hanging indentation regression

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']
            ));
    }
}

Fix ternary / coalesce operator edge case

The last colon here should not be indented:

<?php

$threshold =
    $this->UncertaintyThreshold === null
        ? null
        : (is_array($this->UncertaintyThreshold)
            ? $this->UncertaintyThreshold[$algorithm] ?? null
                : $this->UncertaintyThreshold);

if else alignment

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
}

blank line needed at start of file?

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! :)

Enum "//" comments have extra indentation

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;
}

Improve hanging indentation

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;
}

Document "hanging" vs "overhanging" indentation

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, OR

    • the 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.___]
      

Preserve escaped carriage returns in multiline strings

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
';

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.