PHPCS: PSR1

Personal documentation for the sniffs available in the PHPCS PSR1 Coding Standards to help build an own rulesets and search for error explanations.

Generic.Files.ByteOrderMark | [ref]

Byte Order Marks that may corrupt your application should not be used. These include 0xefbbbf (UTF-8), 0xfeff (UTF-16 BE) and 0xfffe (UTF-16 LE).

Generic.NamingConventions.UpperCaseConstantName | [ref]

Constants should always be all-uppercase, with underscores to separate words.

Valid: all uppercase Invalid: mixed case
define('FOO_CONSTANT', 'foo');

class FooClass
{
    const FOO_CONSTANT = 'foo';
}
define('Foo_Constant', 'foo');

class FooClass
{
    const foo_constant = 'foo';
}

Generic.PHP.DisallowAlternativePHPTags | [ref]

Always use <?php ?> to delimit PHP code, do not use the ASP <% %> style tags nor the <script language="php"></script> tags. This is the most portable way to include PHP code on differing operating systems and setups.

Generic.PHP.DisallowShortOpenTag | [ref]

Always use <?php ?> to delimit PHP code, not the <? ?> shorthand. This is the most portable way to include PHP code on differing operating systems and setups.

PSR1.Classes.ClassDeclaration | [ref]

Each class must be in a file by itself and must be under a namespace (a top-level vendor name).

Valid: One class in a file. Invalid: Multiple classes in a single file.
<?php
namespace Foo;

class Bar {
}
<?php
namespace Foo;

class Bar {
}

class Baz {
}
Valid: A vendor-level namespace is used. Invalid: No namespace used in file.
<?php
namespace Foo;

class Bar {
}
<?php
class Bar {
}

PSR1.Files.SideEffects | [ref]

A php file should either contain declarations with no side effects, or should just have logic (including side effects) with no declarations.

Valid: A class defined in a file by itself. Invalid: A class defined in a file with other code.
<?php
class Foo
{
}
<?php
class Foo
{
}

echo "Class Foo loaded."

PSR1.Methods.CamelCapsMethodName | [ref]

Method names MUST be declared in camelCase.

Valid: method name in camelCase. Invalid: method name not in camelCase.
class Foo
{
    private function doBar()
    {
    }
}
class Foo
{
    private function do_bar()
    {
    }
}

Squiz.Classes.ValidClassName | [ref]

avatar

Looking for a developer who
truly cares about your business?

My team and I provide expert consultations, top-notch coding, and comprehensive audits to elevate your success.

Feedback

How satisfied you are after reading this article?