Commit 5b1bde2c authored by Guillaume Perréal's avatar Guillaume Perréal

Met à niveau la configuration de développement du package.

Utilisation des scripts composer.
parent f4566946
%package% - Bundle de gestion de fichiers intégrée à Symfony et Twitter-Bootstrap.
Copyright (C) %yearRange% Irstea <dsi.poleis.contact@lists.irstea.fr>
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License and the GNU
Lesser General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
devtools/editorconfig
\ No newline at end of file
# editorconfig.org
root=true
[*]
charset=utf-8
end_of_line=lf
indent_size=4
indent_style=space
insert_final_newline=true
tab_width=4
trim_trailing_whitespace=true
[*.{js,yml,yaml,json,ts}]
indent_size=2
tab_width=2
[Makefile]
indent_style=tab
[*.md]
max_line_length = off
trim_trailing_whitespace = false
<?php
define('COMMIT_CACHE', '.php_cs.commit-cache');
<?php declare(strict_types = 1);
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('node_modules')
->exclude('cache')
->exclude('var')
->files()
->name('*.php')
->name('console')
->name('.php_cs.dist')
->in('.');
$yearRange = getGitCommitYears();
$ruleSets = ['@PSR2' => true, '@Symfony' => true];
$rules = [
// Configuration && overrides
'binary_operator_spaces' => ['align_double_arrow' => true],
'blank_line_after_opening_tag' => false,
'concat_space' => ['spacing' => 'one'],
'method_argument_space' => ['ensure_fully_multiline' => true],
// Risky
'is_null' => ['use_yoda_style' => false],
'non_printable_character' => ['use_escape_sequences_in_strings' => true],
// Safe
'align_multiline_comment' => true,
'array_syntax' => ['syntax' => 'short'],
'general_phpdoc_annotation_remove' => ['annotations' => ['author', 'package']],
'no_multiline_whitespace_before_semicolons' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'yoda_style' => false,
'self_accessor' => false,
'header_comment' => [
'commentType' => 'comment',
'location' => 'after_declare_strict',
'separate' => 'bottom',
'header' => <<<HEADER
irstea/file-upload-bundle provides services and widgets to manage user-submitted files.
Copyright (C) $yearRange IRSTEA
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License and the GNU
Lesser General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
HEADER
,
],
];
$phpVersion = findComposerPhpReq();
if ($phpVersion >= 5.6) {
$ruleSets['@PHP56Migration'] = true;
}
if ($phpVersion >= 7.0) {
$ruleSets['@PHP70Migration'] = true;
$rules['declare_strict_types'] = true;
}
if ($phpVersion >= 7.1) {
$ruleSets['@PHP71Migration'] = true;
}
echo "Rulesets: " . implode(', ', array_keys($ruleSets)) . ".\n";
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setUsingCache(false)
return Irstea\CS\Config::create()
->setIndent(' ')
->setLineEnding("\n")
->setRules(array_merge($ruleSets, $rules))
->setFinder($finder);
/**
* @return string
*/
function getGitCommitYears(): string
{
return getCachedValue('years', function () {
echo "Examining git history...\n";
$last = date('Y');
$first = exec('git log --format=%cd --date=format:%Y --date-order | tail -n1') ?? $last;
return (null !== $last && $last !== $first) ? "$first-$last" : $first;
});
}
/**
* @return float
*/
function findComposerPhpReq()
{
return getCachedValue('php-req', function () {
if (file_exists('composer.json')) {
$data = json_decode(file_get_contents('composer.json'), true);
if (is_array($data) && isset($data['require']['php'])) {
if (preg_match('/(?:>=?|\^|~)\s*([57]\.[0-9])/', $data['require']['php'], $groups)) {
return (float) $groups[1];
}
}
}
return 5.6;
});
}
/**
* @param string $key
* @param callable $produce
*
* @return string|int|bool|array
*/
function getCachedValue($key, $produce)
{
static $commit = null;
if (null === $commit) {
$commit = trim(`git rev-parse HEAD`);
}
if (file_exists(COMMIT_CACHE) && filemtime(COMMIT_CACHE) >= filemtime(__FILE__)) {
$cache = json_decode(file_get_contents(COMMIT_CACHE), true);
} else {
$cache = [];
}
if (!isset($cache[$commit][$key])) {
if (!isset($cache[$commit])) {
$cache[$commit] = [];
}
$cache[$commit][$key] = $produce();
file_put_contents(COMMIT_CACHE, json_encode($cache));
}
return $cache[$commit][$key];
}
// vim:filetype=php
......@@ -48,10 +48,22 @@
"xenolope/quahog": "^2.1"
},
"require-dev": {
"phpunit/phpunit": "^7"
"hexmedia/yaml-linter": "^0.2",
"irstea/composer-require-checker-shim": "^2.0",
"irstea/php-cs-fixer-config": "^1.0",
"irstea/phpcpd-shim": "^3.0",
"irstea/phploc-shim": "^4.0",
"irstea/phpmd-config": "^1.0",
"irstea/phpstan-config": "^1.1",
"jakub-onderka/php-parallel-lint": "^1.0",
"pcov/clobber": "^2.0",
"phpunit/phpunit": "^7.0"
},
"config": {
"sort-packages": true
"sort-packages": true,
"platform": {
"php": "7.1.30"
}
},
"extra": {
"branch-alias": {
......@@ -65,5 +77,32 @@
".git*",
".idea"
]
},
"scripts": {
"post-install-cmd": "@clobber-phpunit",
"post-update-cmd": "@clobber-phpunit",
"clobber-phpunit": "[ -d vendor/phpunit/phpunit ] && vendor/bin/pcov . clobber",
"fix-cs": "vendor/bin/php-cs-fixer fix --verbose",
"phploc": "vendor/bin/phploc src tests --exclude tests/Fixtures/var",
"test": [
"@test:lint",
"@test:php-cs-fixer",
"@test:phpcpd",
"@test:phpmd",
"@test:phpstan",
"@test:composer-require-checker",
"@test:phpunit"
],
"test:lint": [
"vendor/bin/parallel-lint -j $(nproc) --exclude tests/Fixtures/var src tests ",
"vendor/bin/yaml-lint src"
],
"test:php-cs-fixer": "vendor/bin/php-cs-fixer fix --dry-run --verbose",
"test:phpcpd": "vendor/bin/phpcpd --fuzzy src tests --exclude=Fixtures/var",
"test:composer-require-checker": "vendor/bin/composer-require-checker",
"test:phpmd": "vendor/bin/phpmd src,tests text ./phpmd-ruleset.xml --suffixes=php",
"test:phpstan": "vendor/bin/phpstan analyse --no-progress",
"test:phpunit": "vendor/bin/phpunit"
}
}
editorconfig
\ No newline at end of file
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/git-commands/git-subrepo#readme
;
[subrepo]
remote = git@gitlab-ssh.irstea.fr:pole-is/devtools-config.git
branch = master
commit = 0c931beb09149c83b9defadd3f96284e7c91ba44
parent = 06988e9db1b139f8a6baf91aa1007c717451455a
cmdver = 0.3.1
Ce dépôt contient la configuration par défaut de quelques outils utiles pendant le développement.
Installation
============
Cloner le dossier dans $HOME/.config/devtools.
```bash
git clone git@gitlab-ssh.irstea.fr:pole-is/devtools-config.git $HOME/.config/devtools
```
Utilisation
Globale
-------
Créer des liens symboliques dans $HOME pour .editorconf et phpcs.xml.dist:
```bash
ln -snf $HOME/.config/devtools/php/phpcs.xml.dist $HOME/phpcs.xml.dist
ln -snf $HOME/.config/devtools/.editorconfig $HOME/.editorconfig
```
Enfin, configurer PHPStorm pour utiliser les fichiers $HOME/.config/devtools/php/phpcs.xml.dist et $HOME/.config/devtools/php/phpmd-ruleset.xml pour phpcs et phpmd respectivement.
Par projet
----------
php-cs-fixer nécessite de copier le fichier directement dans le projet :
```bash
cp $HOME/.config/devtools/php/.php_cs.dist chemin/vers/mon/projet
```
Pour phpcs et phpmd, c'est également nécessaire pour l'intégration continue.
Outils
======
* [editorconfig](): permet de configurer globalement l'encodage, l'indentation et la gestion des espaces.
* [phpcs](https://github.com/squizlabs/PHP_CodeSniffer): vérification du style de codage.
* [php-cs-fixer](https://github.com/FriendsOfPhp/PHP-CS-Fixer): cousin de phpcs, mais qui permet de corriger les fichiers et inclut également des aide pour migrer d'une version de PHP à une autre.
* [phpmd](https://phpmd.org/): détection de bugs potentielles, de fonctions/classes trop complexes, ...
# editorconfig.org
root=true
[*]
charset=utf-8
end_of_line=lf
indent_size=4
indent_style=space
insert_final_newline=true
tab_width=4
trim_trailing_whitespace=true
[*.{js,yml,json,ts}]
indent_size=2
tab_width=2
[Makefile]
indent_style=tab
[*.md]
max_line_length = off
trim_trailing_whitespace = false
.defaults: &defaults
tags: [ docker ]
image: isdevtools.irstea.fr/poleis/php-analysis:7.1
phploc:
<<: *defaults
script: phploc src
lint:
<<: *defaults
script:
- parallel-lint -j $(nproc) src
- twig-lint lint src
- yaml-lint src
php-cs-fixer:
<<: *defaults
script: php-cs-fixer fix --dry-run --verbose
phpcpd:
<<: *defaults
script: phpcpd --fuzzy src
.with-vendors: &with-vendors
<<: *defaults
before_script:
- composer install --prefer-dist --no-progress --no-suggest
cache:
key: "$CI_COMMIT_REF_NAME"
paths:
- /composer/cache
composer-require-checker:
<<: *with-vendors
script: composer-require-checker
phpmd:
<<: *with-vendors
script: phpmd src text ./phpmd-ruleset.xml --suffixes=php
php_cs.dist
\ No newline at end of file
<?php
define('COMMIT_CACHE', '.php_cs.commit-cache');
$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->exclude('node_modules')
->exclude('cache')
->files()
->name('*.php')
->in('.');
$yearRange = getGitCommitYears();
$ruleSets = ['@PSR2' => true, '@Symfony' => true];
$rules = [
// Configuration && overrides
'binary_operator_spaces' => ['align_double_arrow' => true],
'blank_line_after_opening_tag' => false,
'concat_space' => ['spacing' => 'one'],
'method_argument_space' => ['ensure_fully_multiline' => true],
// Risky
'is_null' => ['use_yoda_style' => false],
'non_printable_character' => ['use_escape_sequences_in_strings' => true],
// Safe
'align_multiline_comment' => true,
'array_syntax' => ['syntax' => 'short'],
'general_phpdoc_annotation_remove' => ['annotations' => ['author', 'package']],
'no_multiline_whitespace_before_semicolons' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_annotation_without_dot' => true,
'phpdoc_order' => true,
'semicolon_after_instruction' => true,
'yoda_style' => false,
'self_accessor' => false,
'header_comment' => [
'commentType' => 'comment',
'location' => 'after_declare_strict',
'separate' => 'bottom',
'header' => <<<HEADER
irstea/file-upload-bundle provides services and widgets to manage user-submitted files.
Copyright (C) $yearRange IRSTEA
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License and the GNU
Lesser General Public License along with this program. If not, see
<https://www.gnu.org/licenses/>.
HEADER
,
],
];
$phpVersion = findComposerPhpReq();
if ($phpVersion >= 5.6) {
$ruleSets['@PHP56Migration'] = true;
}
if ($phpVersion >= 7.0) {
$ruleSets['@PHP70Migration'] = true;
$rules['declare_strict_types'] = true;
}
if ($phpVersion >= 7.1) {
$ruleSets['@PHP71Migration'] = true;
}
echo "Rulesets: " . implode(', ', array_keys($ruleSets)) . ".\n";
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setUsingCache(false)
->setIndent(' ')
->setLineEnding("\n")
->setRules(array_merge($ruleSets, $rules))
->setFinder($finder);
/**
* @return string
*/
function getGitCommitYears(): string
{
return getCachedValue('years', function () {
echo "Examining git history...\n";
$last = date('Y');
$first = exec('git log --format=%cd --date=format:%Y --date-order | tail -n1') ?? $last;
return (null !== $last && $last !== $first) ? "$first-$last" : $first;
});
}
/**
* @return float
*/
function findComposerPhpReq()
{
return getCachedValue('php-req', function () {
if (file_exists('composer.json')) {
$data = json_decode(file_get_contents('composer.json'), true);
if (is_array($data) && isset($data['require']['php'])) {
if (preg_match('/(?:>=?|\^|~)\s*([57]\.[0-9])/', $data['require']['php'], $groups)) {
return (float) $groups[1];
}
}
}
return 5.6;
});
}
/**
* @param string $key
* @param callable $produce
*
* @return string|int|bool|array
*/
function getCachedValue($key, $produce)
{
static $commit = null;
if (null === $commit) {
$commit = trim(`git rev-parse HEAD`);
}
if (file_exists(COMMIT_CACHE) && filemtime(COMMIT_CACHE) >= filemtime(__FILE__)) {
$cache = json_decode(file_get_contents(COMMIT_CACHE), true);
} else {
$cache = [];
}
if (!isset($cache[$commit][$key])) {
if (!isset($cache[$commit])) {
$cache[$commit] = [];
}
$cache[$commit][$key] = $produce();
file_put_contents(COMMIT_CACHE, json_encode($cache));
}
return $cache[$commit][$key];
}
// vim:filetype=php
<?xml version="1.0"?>
<ruleset name="Irstea" namespace="Irstea\Standard">
<description>Coding standard of Irstea, pôle informatique scientifique</description>
<file>src</file>
<arg name="encoding" value="utf-8"/>
<arg name="extensions" value="php"/>
<arg name="ignore" value="vendor/*,*/cache/*"/>
<arg value="sp"/>
<rule ref="PSR2"/>
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="140"/>
</properties>
</rule>
</ruleset>
<?xml version="1.0"?>
<ruleset name="Irstea PHPMD ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Irstea ruleset</description>
<rule ref="rulesets/controversial.xml/Superglobals" />
<rule ref="rulesets/controversial.xml/CamelCaseClassName"/>
<rule ref="rulesets/controversial.xml/CamelCasePropertyName"/>
<rule ref="rulesets/design.xml/ExitExpression" />
<rule ref="rulesets/design.xml/EvalExpression" />
<rule ref="rulesets/design.xml/GotoStatement" />
<rule ref="rulesets/design.xml/DevelopmentCodeFragment">
<property name="unwanted-functions" value="var_dump,print_r,debug_zval_dump,debug_print_backtrace,dump" />
</rule>
<rule ref="rulesets/naming.xml/ShortMethodName" />
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass" />
<rule ref="rulesets/naming.xml/ConstantNamingConventions" />
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod" />
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField" />
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>node_modules/</exclude-pattern>
<exclude-pattern>.*/cache/</exclude-pattern>
</ruleset>
devtools/php/phpmd-ruleset.xml
\ No newline at end of file
<?xml version="1.0"?>
<ruleset name="Theme-Bundle ruleset"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd">
<description>Use the loose ruleset of irstea/phpmd-config.</description>
<exclude-pattern>vendor/</exclude-pattern>
<exclude-pattern>node_modules/</exclude-pattern>
<exclude-pattern>.*/cache/</exclude-pattern>
<exclude-pattern>var/</exclude-pattern>
<rule ref="vendor/irstea/phpmd-config/loose.xml"/>
</ruleset>
includes:
- vendor/irstea/phpstan-config/phpstan.neon
parameters:
level: 1
paths:
- src
- tests
<?php declare(strict_types=1);
/*
* irstea/file-upload-bundle provides services and widgets to manage user-submitted files.
* Copyright (C) 2015-2019 IRSTEA
* irstea/file-upload-bundle - Bundle de gestion de fichiers intégrée à Symfony et Twitter-Bootstrap.
* Copyright (C) 2015-2019 Irstea <dsi.poleis.contact@lists.irstea.fr>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
......
<?php declare(strict_types=1);
/*
* irstea/file-upload-bundle provides services and widgets to manage user-submitted files.
* Copyright (C) 2015-2019 IRSTEA
* irstea/file-upload-bundle - Bundle de gestion de fichiers intégrée à Symfony et Twitter-Bootstrap.
* Copyright (C) 2015-2019 Irstea <dsi.poleis.contact@lists.irstea.fr>
*