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

Add options to new_with_braces #6514

Merged
merged 1 commit into from
Sep 9, 2022

Conversation

paulbalandan
Copy link
Member

@paulbalandan paulbalandan commented Sep 8, 2022

Description

$ vendor/bin/php-cs-fixer describe new_with_braces     
Description of new_with_braces rule.
All instances created with `new` keyword must (not) be followed by braces.

Fixer is configurable using following options:
* named_class (bool): whether named classes should be followed by parentheses; defaults to true
* anonymous_class (bool): whether anonymous classes should be followed by parentheses; defaults to true

Fixing examples:
 * Example #1. Fixing with the default configuration.
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,4 +1,4 @@
    <?php

   -$x = new X;
   -$y = new class {};
   +$x = new X();
   +$y = new class() {};

   ----------- end diff -----------

 * Example #2. Fixing with configuration: ['anonymous_class' => false].
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,3 +1,3 @@
    <?php

   -$y = new class() {};
   +$y = new class {};

   ----------- end diff -----------

 * Example #3. Fixing with configuration: ['named_class' => false].
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,3 +1,3 @@
    <?php

   -$x = new X();
   +$x = new X;

   ----------- end diff -----------

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't ask me where I picked this up, but I'm accustomed to spaces in between the declaration for anonymous classes:

$banana = new class () extends Config {}

Do we have rules to enforce that one way or another?

@paulbalandan
Copy link
Member Author

It's the space_before_parenthesis option of class_definition rule.

* Example #5. Fixing with configuration: ['space_before_parenthesis' => true].
   ---------- begin diff ----------
   --- Original
   +++ New
   @@ -1,2 +1,2 @@
    <?php
   -$foo = new class(){};
   +$foo = new class () {};

   ----------- end diff -----------

@paulbalandan paulbalandan merged commit 2a8b1ba into codeigniter4:develop Sep 9, 2022
@paulbalandan paulbalandan deleted the new-with-braces branch September 9, 2022 02:46
paulbalandan added a commit to CodeIgniter/coding-standard that referenced this pull request Sep 9, 2022
henryzhang118 added a commit to henryzhang118/CI-standard-code that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants