2

This is my first question on StackOverflow. I'm very happy for that!

I would like to know if there's some phpcs configuration that can override the same line braces rule. For example, when using PHP 8 new constructor promotion feature, in some cases we don't need any content in the constructor. So it would be nice if phpcs could allow this code:

/**
 * UserService constructor
 *
 * @param UserRepository $userRepository
 * @param BillingService $billingService
 */
public function __construct(
    protected UserRepository $userRepository,
    protected BillingService $billingService
) {}

Or even that:

/**
 * BillingService constructor
 *
 * @param BillingRepository $billingRepository
 */
public function __construct(protected BillingRepository $billingRepository) {}

But it raises the warning, in VS Code for example, "Closing brace must be on a line by itself". I don't want to disable the rule, just allow an exception automatically when using constructor promotion.

The phpcs.xml.dist looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
  <rule ref="PSR1"/>
  <rule ref="PSR2">
    <exclude name="PSR1.Methods.CamelCapsMethodName"/>
  </rule>
</ruleset>

Anyone knows how to achieve this?

Thanks for your time. :)

thiagobraga
  • 1,519
  • 2
  • 15
  • 29
  • 1
    Are you running the latest PHPCS release? It seems that they've updated it to support PHP8 9 days ago with version [3.6.0](https://github.com/squizlabs/PHP_CodeSniffer/releases/tag/3.6.0). If that doesn't do the trick there's multiple ways to disable a specific sniff for a file ([source](https://github.com/squizlabs/PHP_CodeSniffer/issues/1179)) – PtrTon Apr 17 '21 at 22:29
  • Thank you, friend. I installed using `composer global require "squizlabs/php_codesniffer=*"`, so I don't know which version was resolved. I edited `composer.json` and `composer update`. I don't know exactly if I need to do anything, but it didn't work. Looking at Github changelog, I added this rule `` in my `phpcs.dist.xml` file, but it still displays the error. Thanks for the help. – thiagobraga Apr 17 '21 at 22:50
  • 2
    To see what version is currently running you can run `composer show` ([see here](https://stackoverflow.com/questions/15185459/how-to-get-list-of-all-installed-packages-along-with-version-in-composer)). As for disabling you might have excluded a different sniff. To find out what exact one is causing the error you could run PHPCS with the `-s` option [as per this comment](https://stackoverflow.com/questions/13730588/php-codesniffer-show-sniff-that-failed). – PtrTon Apr 17 '21 at 23:09
  • Someone created an issue on Github for this specific case, [check here](https://github.com/squizlabs/PHP_CodeSniffer/issues/3291). – thiagobraga Apr 18 '21 at 16:51

0 Answers0