Skip to content

Commit ba8f484

Browse files
committed
Initial commit
0 parents  commit ba8f484

10 files changed

Lines changed: 319 additions & 0 deletions

File tree

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.{js,jsx}]
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Coding standards
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- '[0-9].[0-9]+'
8+
pull_request: ~
9+
10+
jobs:
11+
php-cs-fixer:
12+
name: PHP CS Fixer
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v6
17+
- uses: actions/checkout@v6
18+
with:
19+
repository: netgen/coding-standard
20+
path: vendor/netgen/coding-standard
21+
- uses: docker://oskarstark/php-cs-fixer-ga
22+
with:
23+
args: --diff --dry-run
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Static analysis
2+
3+
on:
4+
push:
5+
branches:
6+
- 'master'
7+
- '[0-9].[0-9]+'
8+
pull_request: ~
9+
10+
jobs:
11+
static-analysis:
12+
name: ${{ matrix.script }}
13+
runs-on: ubuntu-latest
14+
15+
strategy:
16+
fail-fast: false
17+
matrix:
18+
script: ['phpstan']
19+
20+
steps:
21+
- uses: actions/checkout@v6
22+
- uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: '8.4'
25+
coverage: none
26+
27+
# Install Flex as a global dependency to enable usage of extra.symfony.require
28+
# while keeping Flex recipes from applying
29+
- run: composer global config --no-plugins allow-plugins.symfony/flex true
30+
- run: composer global require --no-scripts symfony/flex
31+
32+
- run: composer config extra.symfony.require ~8.0.0
33+
34+
- run: composer update --prefer-dist
35+
36+
- run: composer ${{ matrix.script }}

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
vendor/
3+
.php-cs-fixer.cache
4+
composer.lock
5+
composer.phar
6+
studio.json

.php-cs-fixer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
// To support running PHP CS Fixer via PHAR file (e.g. in GitHub Actions)
4+
require_once __DIR__ . '/lib/PhpCsFixer/Config.php';
5+
6+
return new Netgen\CodingStandard\PhpCsFixer\Config()
7+
->setFinder(
8+
PhpCsFixer\Finder::create()
9+
->exclude(['vendor'])
10+
->in(__DIR__)
11+
)
12+
;

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2015-2026 Netgen
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Netgen Coding Standard
2+
3+
This repository provides a default configuration used by all Netgen projects to
4+
check for coding standards violations. The config is based on PHP CS Fixer.
5+
6+
## Installation
7+
8+
```bash
9+
$ composer require --dev netgen/coding-standard
10+
```
11+
12+
## Usage
13+
14+
Create a `.php-cs-fixer.php` file in the root of your project with the following:
15+
16+
```php
17+
<?php
18+
19+
return new Netgen\CodingStandard\PhpCsFixer\Config()
20+
->setFinder(
21+
PhpCsFixer\Finder::create()
22+
->exclude(['vendor'])
23+
->in(__DIR__)
24+
)
25+
;
26+
```
27+
28+
Run the fixer with:
29+
30+
```bash
31+
$ vendor/bin/php-cs-fixer fix
32+
```
33+
34+
## Overriding existing rules
35+
36+
You can override rules included in this config per project:
37+
38+
```php
39+
<?php
40+
41+
return new Netgen\CodingStandard\PhpCsFixer\Config()
42+
->addRules([
43+
'list_syntax' => ['syntax' => 'long'],
44+
])
45+
->setFinder(
46+
PhpCsFixer\Finder::create()
47+
->exclude(['vendor'])
48+
->in(__DIR__)
49+
)
50+
;
51+
```
52+
53+
## Supporting PHAR distribution of PHP CS Fixer
54+
55+
You can also support running PHAR version of PHP CS Fixer by adding the
56+
following at the top of your `.php-cs-fixer.php` file:
57+
58+
```php
59+
// To support running PHP CS Fixer via PHAR file (e.g. in GitHub Actions)
60+
require_once __DIR__ . '/vendor/netgen/coding-standard/lib/PhpCsFixer/Config.php';
61+
```
62+
63+
This is e.g. useful if you wish to run PHP CS Fixer via GitHub action, which
64+
does not need running `composer install`:
65+
66+
```yaml
67+
# .github/workflows/coding_standards.yml
68+
name: Coding standards
69+
on: [push, pull_request]
70+
71+
jobs:
72+
php-cs-fixer:
73+
name: PHP CS Fixer
74+
runs-on: ubuntu-latest
75+
76+
steps:
77+
- uses: actions/checkout@v6
78+
- uses: actions/checkout@v6
79+
with:
80+
repository: netgen/coding-standard
81+
path: vendor/netgen/coding-standard
82+
- name: PHP CS Fixer
83+
uses: docker://oskarstark/php-cs-fixer-ga
84+
with:
85+
args: --diff --dry-run
86+
```
87+
88+
Check https://github.com/OskarStark/php-cs-fixer-ga for more details.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "netgen/coding-standard",
3+
"description": "Coding standard for Netgen packages",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Netgen",
8+
"homepage": "https://netgen.io"
9+
}
10+
],
11+
"require": {
12+
"php": "^8.4"
13+
},
14+
"require-dev": {
15+
"friendsofphp/php-cs-fixer": "^3.91",
16+
"phpstan/phpstan": "^2.1",
17+
"phpstan/phpstan-strict-rules": "^2.0"
18+
},
19+
"config": {
20+
"allow-plugins": false
21+
},
22+
"scripts": {
23+
"phpstan": "@php vendor/bin/phpstan analyse -c phpstan.neon --level=8 --ansi lib"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Netgen\\CodingStandard\\": "lib/"
28+
}
29+
},
30+
"minimum-stability": "dev",
31+
"prefer-stable": true,
32+
"extra": {
33+
"branch-alias": {
34+
"dev-master": "1.0.x-dev"
35+
}
36+
}
37+
}

lib/PhpCsFixer/Config.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\CodingStandard\PhpCsFixer;
6+
7+
use PhpCsFixer\Config as BaseConfig;
8+
9+
final class Config extends BaseConfig
10+
{
11+
public function __construct(string $name = 'default')
12+
{
13+
parent::__construct($name);
14+
15+
$this->setCodeStyleRules();
16+
}
17+
18+
/**
19+
* @param array<string, mixed> $rules
20+
*/
21+
public function addRules(array $rules): self
22+
{
23+
$this->setRules([...$this->getRules(), ...$rules]);
24+
25+
return $this;
26+
}
27+
28+
private function setCodeStyleRules(): void
29+
{
30+
$this
31+
->setRiskyAllowed(true)
32+
->setRules(
33+
[
34+
'@PER-CS' => true,
35+
'@PER-CS:risky' => true,
36+
'@PhpCsFixer' => true,
37+
'@PhpCsFixer:risky' => true,
38+
39+
// Overrides for rules included in PhpCsFixer rule sets
40+
'concat_space' => ['spacing' => 'one'],
41+
'fully_qualified_strict_types' => ['phpdoc_tags' => []],
42+
'method_chaining_indentation' => false,
43+
'multiline_promoted_properties' => true,
44+
'multiline_whitespace_before_semicolons' => false,
45+
'native_function_invocation' => ['include' => ['@all']],
46+
'no_superfluous_phpdoc_tags' => false,
47+
'no_unset_on_property' => false,
48+
'operator_linebreak' => false,
49+
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
50+
'ordered_types' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
51+
'php_unit_data_provider_method_order' => false,
52+
'php_unit_data_provider_name' => false,
53+
'php_unit_internal_class' => false,
54+
'php_unit_test_case_static_method_calls' => false,
55+
'php_unit_test_class_requires_covers' => false,
56+
'phpdoc_align' => false,
57+
'phpdoc_order' => ['order' => ['param', 'throws', 'return']],
58+
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
59+
'single_line_comment_style' => false,
60+
'trailing_comma_in_multiline' => ['elements' => ['arrays', 'arguments', 'match', 'parameters']],
61+
'yoda_style' => ['equal' => false, 'identical' => false, 'less_and_greater' => false],
62+
63+
// Additional rules
64+
'date_time_immutable' => true,
65+
'declare_strict_types' => true,
66+
'global_namespace_import' => [
67+
'import_classes' => null,
68+
'import_constants' => true,
69+
'import_functions' => true,
70+
],
71+
'heredoc_indentation' => ['indentation' => 'same_as_start'],
72+
'mb_str_functions' => true,
73+
'native_constant_invocation' => true,
74+
'nullable_type_declaration_for_default_null_value' => true,
75+
'static_lambda' => true,
76+
'ternary_to_null_coalescing' => true,
77+
'use_arrow_functions' => true,
78+
],
79+
);
80+
}
81+
}

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
includes:
2+
- vendor/phpstan/phpstan-strict-rules/rules.neon

0 commit comments

Comments
 (0)