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

Readonly class key triggers a warning about declaring new symbols and causing a side effect #3743

Closed
OleksiiBulba opened this issue Jan 9, 2023 · 2 comments

Comments

@OleksiiBulba
Copy link

Describe the bug
I have a readonly class in php 8.2 and PHP_CodeSniffer shows a warning:

FILE: src/AutowireHelper.php
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 | 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
   |         | 9 and the first side effect is on line 9.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Removing readonly keyword helps to fix the issue.

Code sample

readonly class AutowireHelper implements AutowireHelperInterface
{
}

Custom ruleset

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="pcsg-generated-ruleset">
    <file>src/</file>
    <arg name="basepath" value="."/>
    <arg name="colors"/>
    <description>
        Micro Framework -  сode formatting rules.
    </description>
    <rule ref="Generic.CodeAnalysis.EmptyStatement"/>
    <rule ref="Generic.CodeAnalysis.JumbledIncrementer"/>
    <rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
    <rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
    <rule ref="Generic.CodeAnalysis.UselessOverridingMethod"/>
    <rule ref="Generic.ControlStructures.InlineControlStructure"/>
    <rule ref="Generic.Files.ByteOrderMark"/>
    <rule ref="Generic.Files.LineEndings"/>
    <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="120"/>
        </properties>
    </rule>
    <rule ref="Generic.Formatting.DisallowMultipleStatements"/>
    <rule ref="Generic.Formatting.MultipleStatementAlignment"/>
    <rule ref="Generic.Formatting.NoSpaceAfterCast"/>
    <rule ref="Generic.Functions.CallTimePassByReference"/>
    <rule ref="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
    <rule ref="Generic.Metrics.CyclomaticComplexity"/>
    <rule ref="Generic.Metrics.NestingLevel"/>
    <rule ref="Generic.NamingConventions.ConstructorName"/>
    <rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
    <rule ref="Generic.NamingConventions.CamelCapsFunctionName"/>
    <rule ref="Generic.PHP.DeprecatedFunctions"/>
    <rule ref="Generic.PHP.DisallowShortOpenTag"/>
    <rule ref="Generic.PHP.ForbiddenFunctions"/>
    <rule ref="Generic.PHP.LowerCaseConstant"/>
    <rule ref="Generic.PHP.NoSilencedErrors"/>
    <rule ref="Generic.Strings.UnnecessaryStringConcat"/>
    <rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
    <rule ref="MySource.PHP.EvalObjectFactory"/>
    <rule ref="MySource.PHP.GetRequestData"/>
    <rule ref="PEAR.Classes.ClassDeclaration"/>
    <rule ref="PEAR.Commenting.InlineComment"/>
    <rule ref="PEAR.ControlStructures.MultiLineCondition"/>
    <rule ref="PEAR.Files.IncludingFile"/>
    <rule ref="PEAR.Formatting.MultiLineAssignment"/>
    <rule ref="PEAR.Functions.ValidDefaultValue"/>
    <rule ref="PEAR.NamingConventions.ValidClassName"/>
    <rule ref="PEAR.WhiteSpace.ObjectOperatorIndent"/>
    <rule ref="PEAR.WhiteSpace.ScopeClosingBrace"/>
    <rule ref="PEAR.WhiteSpace.ScopeIndent"/>
    <rule ref="PSR1.Classes.ClassDeclaration"/>
    <rule ref="PSR1.Files.SideEffects"/>
    <rule ref="PSR2.Classes.ClassDeclaration"/>
    <rule ref="PSR2.Classes.PropertyDeclaration"/>
    <rule ref="PSR2.ControlStructures.ControlStructureSpacing"/>
    <rule ref="PSR2.ControlStructures.ElseIfDeclaration"/>
    <rule ref="PSR2.ControlStructures.SwitchDeclaration"/>
    <rule ref="PSR2.Files.EndFileNewline"/>
    <rule ref="PSR2.Methods.MethodDeclaration"/>
    <rule ref="PSR2.Namespaces.NamespaceDeclaration"/>
    <rule ref="PSR2.Namespaces.UseDeclaration"/>
</ruleset>

To reproduce
Steps to reproduce the behavior:

  1. Create any readonly class A
  2. Run phpcs A.php ...
  3. See error message displayed
> ./vendor/bin/phpcs --standard=phpcs.xml ./src/

FILE: src/AutowireHelper.php
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 | 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
   |         | 9 and the first side effect is on line 9.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 40ms; Memory: 6MB

Expected behavior
A clear and concise description of what you expected to happen.

Versions (please complete the following information):

  • OS: Ubuntu 20.04
  • PHP: 8.2
  • PHPCS: 3.7.1
  • Standard: custom, see xml file above
@jrfnl
Copy link
Contributor

jrfnl commented Jan 9, 2023

Duplicate #3727 for which PR #3728 is already open.

@OleksiiBulba
Copy link
Author

@jrfnl, sorry, did not checked it( I will close it as duplicate.

@OleksiiBulba OleksiiBulba closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2023
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

No branches or pull requests

2 participants