Skip to content

Commit

Permalink
[CODESTYLE] Replace phpcs with php-cs-fixer (#1030)
Browse files Browse the repository at this point in the history
* composer require --dev friendsofphp/php-cs-fixer:^2

* php-cs-fixer: ignore cache and custom local config file

* php-cs-fixer: initial config

* php-cs-fixer: replace commands in composer

* php-cs-fixer: add PSR12 "as good as it currently gets"

* php-cs-fixer: apply PSR12 to codebase

* tests: add workaround to keep unused imports for snapshot testing

* php-cs-fixer: apply no_unused_imports

* php-cs-fixer: apply array_syntax short

* php-cs-fixer: apply single_quote

* php-cs-fixer: switch ordered_imports sort_algorithm to alpha

Let's be opinionated here for consistency

* php-cs-fixer: split between non-tests and tests and share config

* php-cs-fixer: apply declare_strict_types for tests

* php-cs-fixer: apply fully_qualified_strict_types

* php-cs-fixer: apply space_after_semicolon

* php-cs-fixer: apply trailing_comma_in_multiline_array

* php-cs-fixer: apply trim_array_spaces

* php-cs-fixer: apply unary_operator_spaces

* php-cs-fixer: apply whitespace_after_comma_in_array

* php-cs-fixer: apply native_function_invocation

* php-cs-fixer: apply concat_space

* grumphp: reflect we're using phpcsfixer now

* composer remove --dev squizlabs/php_codesniffer

* gha: simplify fix-style approach

Not really necessary to remove packages and then manually prevent
unrelated commits creeping in

Co-authored-by: Barry vd. Heuvel <barryvdh@gmail.com>
  • Loading branch information
mfn and barryvdh committed Sep 1, 2020
1 parent a96add6 commit 764bc02
Show file tree
Hide file tree
Showing 55 changed files with 308 additions and 215 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/fix-code-style.yml
Expand Up @@ -23,16 +23,11 @@ jobs:
tools: composer:v2

- name: Install dependencies
run: |
composer remove phpro/grumphp vimeo/psalm --no-update --dev
composer update --prefer-dist --no-progress
run: composer update --prefer-dist --no-progress

- run: composer fix-style
continue-on-error: true

# Revert manual modifications so they don't get commited 💥
- run: git checkout -- composer.json

- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: composer fix-style
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
@@ -1,5 +1,8 @@
.phpunit.result.cache

/.idea
/.php_cs
/.php_cs.cache
/.php_cs.tests.cache
/composer.lock
/vendor
65 changes: 65 additions & 0 deletions .php_cs.common.php
@@ -0,0 +1,65 @@
<?php

// Share common rules between non-test and test files
return [
// PSR12 from https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4943
'@PSR2' => true,
'blank_line_after_opening_tag' => true,
'braces' => [
// Not-yet-implemented
// 'allow_single_line_anonymous_class_with_empty_body' => true,
],
'compact_nullable_typehint' => true,
'declare_equal_normalize' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'new_with_braces' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
],
],
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'alpha',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_trait_insert_per_statement' => true,
'ternary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],

// Further quality-of-life improvements
'array_syntax' => [
'syntax' => 'short',
],
'concat_space' => [
'spacing' => 'one',
],
'fully_qualified_strict_types' => true,
'native_function_invocation' => [
'include' => [],
'strict' => true,
],
'no_unused_imports' => true,
'single_quote' => true,
'space_after_semicolon' => true,
'trailing_comma_in_multiline_array' => true,
'trim_array_spaces' => true,
'unary_operator_spaces' => true,
'whitespace_after_comma_in_array' => true,
];
14 changes: 14 additions & 0 deletions .php_cs.dist
@@ -0,0 +1,14 @@
<?php
require __DIR__ . '/vendor/autoload.php';

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('tests');

$config = require __DIR__ . '/.php_cs.common.php';

return PhpCsFixer\Config::create()
->setFinder($finder)
->setRules($config)
->setRiskyAllowed(true)
->setCacheFile(__DIR__ . '/.php_cs.cache');
22 changes: 22 additions & 0 deletions .php_cs.tests.php
@@ -0,0 +1,22 @@
<?php
require __DIR__ . '/vendor/autoload.php';

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/tests')
->exclude('__snapshots__');

$config = require __DIR__ . '/.php_cs.common.php';

// Additional rules for tests
$config = array_merge(
$config,
[
'declare_strict_types' => true,
]
);

return PhpCsFixer\Config::create()
->setFinder($finder)
->setRules($config)
->setRiskyAllowed(true)
->setCacheFile(__DIR__ . '/.php_cs.tests.cache');
12 changes: 9 additions & 3 deletions composer.json
Expand Up @@ -31,13 +31,13 @@
"phpdocumentor/type-resolver": "^1.1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2",
"illuminate/config": "^6 || ^7 || ^8",
"illuminate/view": "^6 || ^7 || ^8",
"mockery/mockery": "^1.3",
"orchestra/testbench": "^4 || ^5 || ^6",
"phpro/grumphp": "^0.19.0",
"spatie/phpunit-snapshot-assertions": "^1.4 || ^2.2 || ^3",
"squizlabs/php_codesniffer": "^3.5",
"vimeo/psalm": "^3.12"
},
"config": {
Expand Down Expand Up @@ -67,8 +67,14 @@
"prefer-stable": true,
"scripts": {
"analyze": "psalm",
"check-style": "phpcs -p --standard=PSR12 config/ resources/ src/ tests/ '--ignore=*__snapshots_*'",
"fix-style": "phpcbf -p --standard=PSR12 config/ resources/ src/ tests/ '--ignore=*__snapshots_*'",
"check-style": [
"php-cs-fixer fix --diff --diff-format=udiff --dry-run",
"php-cs-fixer fix --diff --diff-format=udiff --dry-run --config=.php_cs.tests.php"
],
"fix-style": [
"php-cs-fixer fix",
"php-cs-fixer fix --config=.php_cs.tests.php"
],
"test": "phpunit",
"test-regenerate": "phpunit -d --update-snapshots"
}
Expand Down
39 changes: 19 additions & 20 deletions config/ide-helper.php
@@ -1,6 +1,6 @@
<?php

return array(
return [

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -98,9 +98,9 @@

'include_helpers' => false,

'helper_files' => array(
'helper_files' => [
base_path() . '/vendor/laravel/framework/src/Illuminate/Support/helpers.php',
),
],

/*
|--------------------------------------------------------------------------
Expand All @@ -115,9 +115,9 @@
|
*/

'model_locations' => array(
'model_locations' => [
'app',
),
],

/*
|--------------------------------------------------------------------------
Expand All @@ -128,9 +128,9 @@
|
*/

'ignored_models' => array(
'ignored_models' => [

),
],

/*
|--------------------------------------------------------------------------
Expand All @@ -141,12 +141,12 @@
|
*/

'extra' => array(
'Eloquent' => array('Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'),
'Session' => array('Illuminate\Session\Store'),
),
'extra' => [
'Eloquent' => ['Illuminate\Database\Eloquent\Builder', 'Illuminate\Database\Query\Builder'],
'Session' => ['Illuminate\Session\Store'],
],

'magic' => array(),
'magic' => [],

/*
|--------------------------------------------------------------------------
Expand All @@ -158,9 +158,9 @@
|
*/

'interfaces' => array(
'interfaces' => [

),
],

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -188,9 +188,9 @@
| ),
|
*/
'custom_db_types' => array(
'custom_db_types' => [

),
],

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -226,10 +226,10 @@
| Cast the given "real type" to the given "type".
|
*/
'type_overrides' => array(
'type_overrides' => [
'integer' => 'int',
'boolean' => 'bool',
),
],

/*
|--------------------------------------------------------------------------
Expand All @@ -253,5 +253,4 @@
|
*/
'force_fqn' => false,

);
];
2 changes: 1 addition & 1 deletion grumphp.yml
Expand Up @@ -5,7 +5,7 @@ grumphp:
triggered_by: [php]
metadata:
task: composer_script
phpcs:
phpcsfixer:
script: check-style
triggered_by: [php]
metadata:
Expand Down
24 changes: 12 additions & 12 deletions src/Alias.php
Expand Up @@ -11,13 +11,13 @@

namespace Barryvdh\LaravelIdeHelper;

use Closure;
use ReflectionClass;
use Barryvdh\Reflection\DocBlock;
use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
use Closure;
use Illuminate\Config\Repository as ConfigRepository;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use ReflectionClass;

class Alias
{
Expand All @@ -31,12 +31,12 @@ class Alias
protected $short;
protected $namespace = '__root';
protected $root = null;
protected $classes = array();
protected $methods = array();
protected $usedMethods = array();
protected $classes = [];
protected $methods = [];
protected $usedMethods = [];
protected $valid = false;
protected $magicMethods = array();
protected $interfaces = array();
protected $magicMethods = [];
protected $interfaces = [];
protected $phpdoc = null;

/** @var ConfigRepository */
Expand All @@ -50,7 +50,7 @@ class Alias
* @param array $magicMethods
* @param array $interfaces
*/
public function __construct($config, $alias, $facade, $magicMethods = array(), $interfaces = array())
public function __construct($config, $alias, $facade, $magicMethods = [], $interfaces = [])
{
$this->alias = $alias;
$this->magicMethods = $magicMethods;
Expand Down Expand Up @@ -82,7 +82,7 @@ public function __construct($config, $alias, $facade, $magicMethods = array(), $


if ($facade === '\Illuminate\Database\Eloquent\Model') {
$this->usedMethods = array('decrement', 'increment');
$this->usedMethods = ['decrement', 'increment'];
}
}

Expand Down Expand Up @@ -289,12 +289,12 @@ protected function detectRoot()
//When the database connection is not set, some classes will be skipped
} catch (\PDOException $e) {
$this->error(
"PDOException: " . $e->getMessage() .
'PDOException: ' . $e->getMessage() .
"\nPlease configure your database connection correctly, or use the sqlite memory driver (-M)." .
" Skipping $facade."
);
} catch (\Exception $e) {
$this->error("Exception: " . $e->getMessage() . "\nSkipping $facade.");
$this->error('Exception: ' . $e->getMessage() . "\nSkipping $facade.");
}
}

Expand Down

0 comments on commit 764bc02

Please sign in to comment.