Giter Club home page Giter Club logo

coding-standard's Introduction

Consistence Coding Standard

Complete standard is described in a separate document.

This is a custom coding standard, which is used by all Consistence projects. It is also suitable to be used with any other project or as a foundation for your own standard.

The main objectives of this standard are:

  • Strict (and predictable) code.
  • Prevent common mistakes.
  • Readability over writability (character count).
  • Be friendly to diffs - minimize impact of changes.
  • Do not write unnecessary/unreliable information.

Automatic checks

Automatic checks of this standard are implemented as sniffs for PHP_CodeSniffer (phpcs), but not all the rules are checked yet. PHP_CodeSniffer contains also tool for automatic correction of certain errors phpcbf, but this tool is also not 100% reliable - results produced by this may not conform to this standard.

These checks also include some code analysis tools, such as detecting unreachable code etc. These are not part of the standard, but should be helpful.

Stability

This package uses SemVer with following rules.

Coding Standard document should be considered the source of truth and main object of SemVer.

  • MAJOR version will be incremented if new rules are added to the document.
  • MINOR version will be incremented if new sniffs are implemented to check for existing described rules.
  • PATCH version will be incremented for bug fixing - fixing a bug is considered everything which does not conform to the document - this may even lead to (temporarily) disabling an existing sniff - or part of it, until a better check is available or it is properly fixed.

The implementation of the automatic checks (both custom sniffs and the ruleset.xml file) are not subject to the SemVer and may change over time to accommodate changes in PHP_CodeSniffer and provided default sniffs, which are used also by this standard.

Recommended dependency on this package is on MINOR version (e.g. ~1.0.0), which means effectively:

  • No new rules will be added.
  • New automatic checks may be added.
  • You get fixes for existing automatic checks, or some of them may be disabled, if regressions are found.

Installation & usage

Install package consistence/coding-standard with Composer:

composer require --dev consistence/coding-standard

Then run phpcs with this standard:

vendor/bin/phpcs --standard=vendor/consistence/coding-standard/Consistence/ruleset.xml --extensions=php --encoding=utf-8 -sp src

For further usage options see the PHP_CodeSniffer documentation.

coding-standard's People

Contributors

janlanger avatar krns avatar kukulich avatar mhujer avatar michondr avatar vasekpurchart avatar vrtak-cz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

coding-standard's Issues

PHP 8 Support

Any plans to update this lib to support PHP8? It could be as simple as adding ^7.0|^8.0 to the composer.json config.

Problem with Consistence.Exceptions.ExceptionDeclaration

use Throwable;

class InvalidResponseException extends Exception
{
    public function __construct(Validator $validator, ?Throwable $previous = null)
    {
        // ...
    }

This reports Exception is not chainable. It must have optional \Throwable as last constructor argument and has "Throwable".

FunctionSpacing sniff misconfigured

What is the reason for excluding Squiz.WhiteSpace.FunctionSpacing.After here? With this, you can separate methods by any number of empty lines. May I send PR removing this exclusion?

Coding standard does not specify negation formatting

Currently the negation formatting is not explicitly specified in the standard.

It can be written either:

  • with no spaces if (!is_array($foo))
  • with space after if (! is_array($foo))
  • with spaces around if ( ! is_array($foo))

In the Scalar types chapter there is a code example which includes negation formatted without spaces around:

<?php

if (!is_int($foo)) {
	return (int) $foo;
}

So it would be nice to have it explicitly stated in the coding standard. Maybe eventually also checked automatically (but PHP_CodeSniffer currently offers only SpaceAfterNotSniff).

Closure variables from use are handled same way as its parameters

When using closure with multiple parameters each on new line, sniffer requires every variable in use to be each on new line too.

Example:

$foo = 1;
$bar = 2;

static function (
	bool $param1,
	bool $param2
) use ($foo, $bar): int {
	return $foo + $bar;
}

…leads to…

ERROR | [x] Multi-line use declarations must define one parameter per line
      |     (Squiz.Functions.MultiLineFunctionDeclaration.UseOneParamPerLine)

Expected behaviour:
This should be fine since each parameter is defined on separate line. To put variables in use each on new line indeed let sniffer to pass; but it leads to worse code readability.

Rules for PHP 7 features

PHP 7 added new language features that can be formatted in different ways. It would be great if Consistence can define its way to format them. e.g.

  • declare(strict_types = 1) - just after the PHP or on a new line?
  • Return type hints - FQCN or not?
  • Primitive type hints

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.