Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP8 - Attribute support #5284

Merged
merged 1 commit into from
Dec 4, 2020
Merged

PHP8 - Attribute support #5284

merged 1 commit into from
Dec 4, 2020

Conversation

SpacePossum
Copy link
Contributor

@SpacePossum SpacePossum commented Nov 21, 2020

PHP8 - Attribute support

@SpacePossum SpacePossum marked this pull request as draft November 21, 2020 16:51
@SpacePossum SpacePossum added this to the 2.16.8 milestone Nov 21, 2020
@SpacePossum SpacePossum added kind/enhancement topic/PHP8.0 Related to features available in PHP 8.0+ labels Nov 21, 2020
@SpacePossum SpacePossum mentioned this pull request Nov 21, 2020
62 tasks
@SpacePossum SpacePossum changed the base branch from 2.16 to master November 25, 2020 12:35
@SpacePossum SpacePossum modified the milestones: 2.16.8, 2.17.0 Nov 25, 2020
@b1rdex
Copy link

b1rdex commented Nov 27, 2020

Does this include fixing single_line_comment_style? It converts attributes to a // comment.
Or should I open a separate issue?

@SpacePossum
Copy link
Contributor Author

good point, will be fixed in #5302

@SpacePossum SpacePossum marked this pull request as ready for review December 4, 2020 10:40
@SpacePossum SpacePossum merged commit 513f996 into PHP-CS-Fixer:master Dec 4, 2020
@SpacePossum SpacePossum deleted the 2_16_PHP8_Attribute_support branch December 4, 2020 12:12
@keradus
Copy link
Member

keradus commented Dec 16, 2020

@SpacePossum , should we mark Attributes task in #4702 done ?

@SpacePossum
Copy link
Contributor Author

I think there are a few minors left on this topic.

@keradus
Copy link
Member

keradus commented Dec 16, 2020

would be great to document them ;)

@robfrawley
Copy link

robfrawley commented Jun 6, 2021

@SpacePossum & @keradus: Any idea how I tell this new fixer not to de-indent functions following an attribute? For example, in PHPStorm, they offer the attribute #[Pure] for functions that do not cause any side-effects (one whose logic is fully contained). Running php-cs-fixer on the following block of code causes issues:

<?php

namespace App\Utility\Version\Resolver;

use App\Utility\Version\Immutable\VersionImmutable;
use App\Utility\Version\Immutable\VersionImmutableInterface;
use App\Utility\Version\Nullable\VersionNullable;
use App\Utility\Version\Nullable\VersionNullableInterface;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Process\Process;

class GitVersionResolver extends AbstractVersionResolver
{
    /* other, non-relivant lines of code */

    #[Pure]
    private static function createVersionFromMatches(array $matches): VersionImmutableInterface
    {
        return new VersionImmutable(
            'git',
            $matches['major'] ?? 0,
            $matches['minor'] ?? 0,
            $matches['patch'] ?? 0,
            sprintf('release:%d', $matches['release']),
            $matches['commit']
        );
    }
}

The above code is "fixed" as follows:

<?php

namespace App\Utility\Version\Resolver;

use App\Utility\Version\Immutable\VersionImmutable;
use App\Utility\Version\Immutable\VersionImmutableInterface;
use App\Utility\Version\Nullable\VersionNullable;
use App\Utility\Version\Nullable\VersionNullableInterface;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Process\Process;

class GitVersionResolver extends AbstractVersionResolver
{
    /* other, non-relivant lines of code */

    #[Pure]
 private static function createVersionFromMatches(array $matches): VersionImmutableInterface
 {
     return new VersionImmutable(
            'git',
            $matches['major'] ?? 0,
            $matches['minor'] ?? 0,
            $matches['patch'] ?? 0,
            sprintf('release:%d', $matches['release']),
            $matches['commit']
        );
 }
}

The suggested diff from php-cs-fixer looks like this:

--- Original
+++ New
@@ @@
 
-    #[Pure] private static function createVersionFromMatches(array $matches): VersionImmutableInterface
-    {
-        return new VersionImmutable(
+    #[Pure]
+ private static function createVersionFromMatches(array $matches): VersionImmutableInterface
+ {
+     return new VersionImmutable(
@@ @@
         );
-    }
+ }
 }

Any idea what configuration I need to use to avoid this single-spaced indentation of functions following attributes? Thanks!

FYI: The following is my php-cs-fixer configuration:

<?php

use PhpCsFixer\Config;
use PhpCsFixer\Finder;

$header = <<<HEADER
This file is part of the `src-run/src-run-web` project.

(c) Rob Frawley 2nd <[email protected]>

For the full copyright and license information, please view the LICENSE.md
file that was distributed with this source code.
HEADER;

return (new Config())
    ->setUsingCache(true)
    ->setRiskyAllowed(true)
    ->setHideProgress(false)
    ->setLineEnding("\n")
    ->setIndent('    ')
    ->setCacheFile('.php-cs-fixer.cache')
    ->setFinder(
        (new Finder())
            ->in(__DIR__)
            ->exclude('var')
    )
    ->setRules([
        '@Symfony' => true,
        '@Symfony:risky' => true,
        '@PHPUnit57Migration:risky' => true,
        'align_multiline_comment' => [
            'comment_type' => 'phpdocs_like'
        ],
        'array_indentation' => true,
        'array_syntax' => [
            'syntax' => 'short'
        ],
        'braces' => [
            'allow_single_line_closure' => true,
        ],
        'binary_operator_spaces' => [
            'align_double_arrow' => false,
            'align_equals' => false,
        ],
        'combine_consecutive_issets' => true,
        'combine_consecutive_unsets' => true,
        'concat_space' => [
            'spacing' => 'one',
        ],
        'escape_implicit_backslashes' => true,
        'explicit_indirect_variable' => true,
        'final_internal_class' => true,
        'function_typehint_space' => true,
        'hash_to_slash_comment' => true,
        'header_comment' => [
            'header' => $header, 'separate' => 'both'
        ],
        'heredoc_to_nowdoc' => true,
        'linebreak_after_opening_tag' => true,
        'list_syntax' => [
            'syntax' => 'short',
        ],
        'lowercase_cast' => true,
        'mb_str_functions' => true,
        'method_separation' => true,
        'multiline_whitespace_before_semicolons' => [
            'strategy' => 'no_multi_line',
        ],
        'native_constant_invocation' => false,
        'native_function_invocation' => false,
        'no_multiline_whitespace_before_semicolons' => true,
        'no_php4_constructor' => true,
        'no_short_echo_tag' => true,
        'no_unreachable_default_argument_value' => true,
        'no_useless_else' => true,
        'no_useless_return' => true,
        'no_extra_consecutive_blank_lines' => [
            'curly_brace_block',
            'extra',
            'parenthesis_brace_block',
            'square_brace_block',
            'throw',
            'use',
        ],
        'no_whitespace_before_comma_in_array' => true,
        'no_whitespace_in_blank_line' => true,
        'ordered_class_elements' => ['order' => [
            'use_trait',
            'constant_public',
            'constant_protected',
            'constant_private',
            'property_public',
            'property_protected',
            'property_private',
            'construct',
            'destruct',
            'magic',
            'phpunit',
            'method_public',
            'method_protected',
            'method_private'
        ],],
        'ordered_imports' => true,
        'php_unit_strict' => true,
        'php_unit_no_expectation_annotation' => true,
        'php_unit_test_class_requires_covers' => true,
        'phpdoc_order' => true,
        'phpdoc_summary' => false,
        'psr4' => true,
        'semicolon_after_instruction' => true,
        'short_scalar_cast' => true,
        'single_blank_line_before_namespace' => true,
        'single_quote' => true,
        'strict_comparison' => true,
        'strict_param' => true,
        'trim_array_spaces' => true,
        'unary_operator_spaces' => true,
        'whitespace_after_comma_in_array' => true,
    ])
;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement topic/PHP8.0 Related to features available in PHP 8.0+
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants