Php_codesniffer: File with "defined() or define()" syntax triggers PSR1.Files.SideEffects.FoundWithSymbols

Created on 5 Jul 2018  路  3Comments  路  Source: squizlabs/PHP_CodeSniffer

<?php
defined('APP_BASE_URL') or define('APP_BASE_URL', '/');

triggers
WARNING
A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 2 and the first side effect is on line 2. (PSR1.Files.SideEffects.FoundWithSymbols)

Bug

Most helpful comment

So maybe I need to detect function calls followed by a logical operator and allow those to pass.

This would be making the assumption that the function being called doesn't do anything else except return a boolean. I think that's a big assumption, so I'll just limit this to defined() for now.

All 3 comments

The sniff is being tripped up by the fact that you're not using a structured conditional statement to define your constant as it explicitly looks for those. I might be able to add defined() into that list to get this particular syntax working, but it wouldn't detect user-defined functions. E.g.,

<?php
inProd() or define('IN_UAT', true);

So maybe I need to detect function calls followed by a logical operator and allow those to pass.

So maybe I need to detect function calls followed by a logical operator and allow those to pass.

This would be making the assumption that the function being called doesn't do anything else except return a boolean. I think that's a big assumption, so I'll just limit this to defined() for now.

Thanks for the report. This issue is now fixed and will be released in 3.3.1

Was this page helpful?
0 / 5 - 0 ratings