apsg/coding-standards

There is no license information available for the latest version (dev-master) of this package.

Coding standardsfor PINT

dev-master 2023-03-06 12:36 UTC

This package is auto-updated.

Last update: 2024-05-06 15:27:19 UTC


README

Usage

We can pass the path of the config file to Pint: https://laravel.com/docs/9.x/pint#configuring-pint

so after this package is installed:

composer require apsg/coding-standards

one can tell Pint to use this config:

./vendor/bin/pint --config vendor/apsg/coding-standards/pint.json

Alternatively - one can just copy the pint.json file and use it as a template in it's own projects.

Rules descriptions:

Namespace

"single_blank_line_before_namespace": false,
"no_blank_lines_before_namespace": true,
"blank_line_after_opening_tag": false,

Those three ( 1, 2, 3 ) remove extra line before namespace. We don't look at this part of the file anyway, so this extra line is surplus in my opinion.

Before

<?php

namespace App\Domains\Admin\Controllers;

After:

<?php
namespace App\Domains\Admin\Controllers;

Trait imports

"single_trait_insert_per_statement": true, 

Thanks to single-line trait imports we immediately see all of them - there is no import that hides in this right-hand side twilight zone.

Also this allows to simply comment out single import while coding/debugging.

Before:

class Material extends Model implements HasMedia
{
    use HasStates, HasFactory, HasSlug, InteractsWithMedia;
...

After:

class Material extends Model implements HasMedia
{
    use HasStates;
    use HasFactory;
    use HasSlug;
    use InteractsWithMedia;
...

Spaces

Rules:

"not_operator_with_successor_space": false,
"cast_spaces": {
    "space": "none"
},
"concat_space": {
    "spacing": "one"
},

Before:

if(! $boolVariable) ...

$someString = $a.'-'.$b.'suffix';

$floatVariable = (float) '2.13'; 

After:

if(!$boolVariable) ...

$someString = $a . '-' . $b . 'suffix';

$floatVariable = (float)'2.13'; 

Alignment

Following rule for me is a must - especially when building large arrays (like in transformers). It also reduces visual clutter

"binary_operator_spaces": {
    "operators": {
        "=>": "align"
    }
},

Before:

return [
    'id' => $attachment->id,
    'name' => $attachment->name,
    'mime' => $attachment->mime,
    'url' => $attachment->downloadUrl(),
    'element' => $attachment->element,
    'copies' => $attachment->copies,

After:

return [
    'id'      => $attachment->id,
    'name'    => $attachment->name,
    'mime'    => $attachment->mime,
    'url'     => $attachment->downloadUrl(),
    'element' => $attachment->element,
    'copies'  => $attachment->copies,

Related to previous rule is another one, that reduces visual clutter in php-docs:

"phpdoc_align": {
    "align": "vertical",
    "tags": [
        "method",
        "param",
        "property",
        "property-read",
        "property-write",
        "return",
        "throws",
        "type",
        "var"
    ]
},

Before:

/**
 * @property int $id
 * @property int $user_id
 * @property string $title
 * @property string $slug
 * @property string|null $description
 * @property-read Collection|Attachment[] $attachments
 * @property-read User $owner
 */
class Material extends Model implements HasMedia

After:

/**
 * @property      int                     $id
 * @property      int                     $user_id
 * @property      string                  $title
 * @property      string                  $slug
 * @property      string|null             $description
 * @property-read Collection|Attachment[] $attachments
 * @property-read User                    $owner
 */
class Material extends Model implements HasMedia

Blank lines

Rule

"no_extra_blank_lines": {
    "tokens": [
        "extra",
        "square_brace_block",
        "return"
    ]
}

This extends default behaviour (extra) and removes surplus blank lines:

  • inside of curly braces []
  • after return statement