narrowspark/coding-standard

The Narrowspark Coding Standard is a set of phpstan, psalm, infection, rector and php-cs-fixer rules applied to all Narrowspark projects.


Keywords
config, cs, narrowspark, php-cs-fixer, cs-fixer, coding-standard, PHPStan, psalm, rector, changelog, library, php
License
MIT

Documentation

Narrowspark Coding Standard

The Narrowspark Coding Standard for all Narrowspark projects.

Installation

composer require narrowspark/coding-standard

Use

PHPstan

Create a configuration file phpstan.neon

Add the rules manually or use composer require --dev phpstan/extension-installer.

If you don’t want to use phpstan/extension-installer, include extension.neon in your project’s PHPStan config:

includes:
    - vendor/narrowspark/coding-standard/base_rules.neon

Or

includes:
    - vendor/ekino/phpstan-banned-code/extension.neon
    - vendor/phpstan/phpstan-deprecation-rules/rules.neon
    - vendor/phpstan/phpstan-mockery/extension.neon
    - vendor/phpstan/phpstan-phpunit/extension.neon
    - vendor/phpstan/phpstan-phpunit/rules.neon
    - vendor/phpstan/phpstan-strict-rules/rules.neon
    - vendor/phpstan/phpstan/conf/bleedingEdge.neon
    - vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
    - vendor/slam/phpstan-extensions/conf/slam-rules.neon

parameters:
    level: max
    inferPrivatePropertyTypeFromConstructor: true

    excludes_analyse:
        - vendor

    banned_code:
        # enable detection of `use Tests\Foo\Bar` in a non-test file
        use_from_tests: true

Follow the links to check, how to configure the rules:

PHP-CS-Fixer

Create a configuration file .php_cs in the root of your project with this content:

<?php
declare(strict_types=1);
use Narrowspark\CS\Config\Config;

$config = new Config();
$config->getFinder()
    ->files()
    ->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')
    ->exclude('build')
    ->exclude('vendor')
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;

Info:

The used php-cs-fixer rules.

For more information, take a look on php-cs-fixer-config.

Psalm

Add your config with this command.

./vendor/bin/psalm --init

Now you need to add the phpunit and mockery plugin to the created psalm.xml

...

+<plugins>
+  <pluginClass class="Psalm\MockeryPlugin\Plugin" />
+  <pluginClass class="Psalm\PhpUnitPlugin\Plugin" />
+</plugins>

...

Infection

The first time you run Infection for your project, it will ask you questions to create a config file infection.json.dist

Changelog

Create this labels on github Added, Changed, Deprecated, Removed, Fixed, Security

Create a configuration file .changelog in the root of your project with this content:

<?php
declare(strict_types=1);

use ChangelogGenerator\ChangelogConfig;

return [
    (new ChangelogConfig(
        '{organisation name}',
        '{repository name}',
        '{your next version}',
        ['Added', 'Changed', 'Deprecated', 'Removed', 'Fixed', 'Security']
    ))
];

Create a CHANGELOG.md file and put this on the top.

# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

Info: for more information, take a look on keepachangelog.

Composer

Then edit your composer.json file and add these scripts:

{
  "scripts": {
    "cs": "php-cs-fixer fix",
    "phpstan": "phpstan analyse -c phpstan.neon -l 7 src --memory-limit=-1",
    "psalm": "psalm",
     "changelog":  "changelog-generator generate --config=\".changelog\" --file --append"
  }
}

Tip: if some processes taking longer than the default composer process-timeout: 300 you can add this to in your composer.json

{
    "config": {
        "process-timeout": 2000 #choose you need time
    }
}

Add .php_cs.cache to your .gitignore file.

Versioning

This library follows semantic versioning, and additions to the code ruleset are performed in major releases.

Testing

composer test

Contributing

If you would like to help take a look at the list of issues and check our CONTRIBUTING.md guide.

Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

The Narrowspark Coding Standard is open-sourced software licensed under the MIT license