Skip to content

Commit cfa526b

Browse files
committed
Added coding standard and analysis tools
1 parent 6a6b589 commit cfa526b

File tree

8 files changed

+194
-6
lines changed

8 files changed

+194
-6
lines changed

.github/workflows/pr.yaml

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
on: pull_request
2+
name: PR Review
3+
jobs:
4+
test-composer-files:
5+
name: Validate composer
6+
runs-on: ubuntu-latest
7+
strategy:
8+
matrix:
9+
php-versions: [ '7.4', '8.0', '8.1' ]
10+
dependency-version: [ prefer-lowest, prefer-stable ]
11+
steps:
12+
- uses: actions/checkout@master
13+
- name: Setup PHP, with composer and extensions
14+
uses: shivammathur/setup-php@v2
15+
with:
16+
php-version: ${{ matrix.php-versions }}
17+
extensions: json
18+
coverage: none
19+
tools: composer:v2
20+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
21+
- name: Get composer cache directory
22+
id: composer-cache
23+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
24+
- name: Cache dependencies
25+
uses: actions/cache@v2
26+
with:
27+
path: ${{ steps.composer-cache.outputs.dir }}
28+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
29+
restore-keys: ${{ runner.os }}-composer-
30+
- name: Validate composer files
31+
run: |
32+
composer validate --strict composer.json
33+
# Check that dependencies resolve.
34+
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
35+
36+
php-check-coding-standards:
37+
name: PHP - Check Coding Standards
38+
runs-on: ubuntu-latest
39+
strategy:
40+
matrix:
41+
php-versions: [ '7.4', '8.0', '8.1' ]
42+
dependency-version: [ prefer-lowest, prefer-stable ]
43+
steps:
44+
- uses: actions/checkout@master
45+
- name: Setup PHP, with composer and extensions
46+
uses: shivammathur/setup-php@v2
47+
with:
48+
php-version: ${{ matrix.php-versions }}
49+
extensions: json
50+
coverage: none
51+
tools: composer:v2
52+
# https://github.com/shivammathur/setup-php#cache-composer-dependencies
53+
- name: Get composer cache directory
54+
id: composer-cache
55+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
56+
- name: Cache dependencies
57+
uses: actions/cache@v2
58+
with:
59+
path: ${{ steps.composer-cache.outputs.dir }}
60+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
61+
restore-keys: ${{ runner.os }}-composer-
62+
- name: Install Dependencies
63+
run: |
64+
composer install --no-interaction --no-progress
65+
- name: PHPCS
66+
run: |
67+
composer coding-standards-check/phpcs
68+
69+
# php-code-analysis:
70+
# name: PHP - Code analysis
71+
# runs-on: ubuntu-latest
72+
# strategy:
73+
# matrix:
74+
# php-versions: [ '7.4', '8.0', '8.1' ]
75+
# dependency-version: [ prefer-lowest, prefer-stable ]
76+
# steps:
77+
# - uses: actions/checkout@master
78+
# - name: Setup PHP, with composer and extensions
79+
# uses: shivammathur/setup-php@v2
80+
# with:
81+
# php-version: ${{ matrix.php-versions }}
82+
# extensions: json, gd
83+
# coverage: none
84+
# tools: composer:v2
85+
# # https://github.com/shivammathur/setup-php#cache-composer-dependencies
86+
# - name: Get composer cache directory
87+
# id: composer-cache
88+
# run: echo "::set-output name=dir::$(composer config cache-files-dir)"
89+
# - name: Cache dependencies
90+
# uses: actions/cache@v2
91+
# with:
92+
# path: ${{ steps.composer-cache.outputs.dir }}
93+
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
94+
# restore-keys: ${{ runner.os }}-composer-
95+
# - name: drupal-check
96+
# run: |
97+
# # We need a Drupal project to run drupal-check (cf. https://github.com/mglaman/drupal-check#usage)
98+
# # Install Drupal
99+
# composer --no-interaction create-project drupal/recommended-project:^9 --stability=dev drupal
100+
# # Copy our module source code into the Drupal module folder.
101+
# mkdir -p drupal/web/modules/contrib/os2forms_rest_api
102+
# cp -r os2forms_rest_api.* composer.json src drupal/web/modules/contrib/os2forms_rest_api
103+
# # Add our module as a composer repository.
104+
# composer --no-interaction --working-dir=drupal config repositories.os2forms/os2forms_rest_api path web/modules/contrib/os2forms_rest_api
105+
# # Restore Drupal composer repository.
106+
# composer --no-interaction --working-dir=drupal config repositories.drupal composer https://packages.drupal.org/8
107+
108+
# # Require our module.
109+
# composer --no-interaction --working-dir=drupal require 'os2forms/os2forms_rest_api:*'
110+
111+
# # Check code
112+
# # composer --no-interaction --working-dir=drupal require --dev drupal/core-dev
113+
# cd drupal/web/modules/contrib/os2forms_rest_api
114+
# # @fixme
115+
# # Remove our non-dev dependencies to prevent dupplicated Drupal installation
116+
# # PHP Fatal error: Cannot redeclare drupal_get_filename() (previously declared in /home/runner/work/os2forms_rest_api/os2forms_rest_api/drupal/web/modules/contrib/os2forms_rest_api/vendor/drupal/core/includes/bootstrap.inc:190) in /home/runner/work/os2forms_rest_api/os2forms_rest_api/drupal/web/core/includes/bootstrap.inc on line 190
117+
# sed -i '/"require":/,+3d' composer.json
118+
# composer --no-interaction install
119+
# composer code-analysis

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

.php-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
7.4

composer.json

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,44 @@
1111
],
1212
"minimum-stability": "dev",
1313
"prefer-stable": true,
14+
"repositories": [
15+
{
16+
"type": "composer",
17+
"url": "https://packages.drupal.org/8"
18+
}
19+
],
20+
"require": {
21+
"drupal/key_auth": "^2.0"
22+
},
1423
"require-dev": {
24+
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2",
1525
"drupal/coder": "^8.3",
16-
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1"
26+
"mglaman/drupal-check": "^1.4"
1727
},
1828
"scripts": {
19-
"coding-standards-check": "phpcs --standard=phpcs.xml.dist",
20-
"coding-standards-apply": "phpcbf --standard=phpcs.xml.dist"
29+
"code-analysis/drupal-check": [
30+
"vendor/bin/drupal-check --deprecations --analysis --exclude-dir=vendor *.* src"
31+
],
32+
"code-analysis": [
33+
"@code-analysis/drupal-check"
34+
],
35+
"coding-standards-check/phpcs": [
36+
"vendor/bin/phpcs --standard=phpcs.xml.dist"
37+
],
38+
"coding-standards-check": [
39+
"@coding-standards-check/phpcs"
40+
],
41+
"coding-standards-apply/phpcs": [
42+
"vendor/bin/phpcbf --standard=phpcs.xml.dist"
43+
],
44+
"coding-standards-apply": [
45+
"@coding-standards-apply/phpcs"
46+
]
47+
},
48+
"config": {
49+
"sort-packages": true,
50+
"allow-plugins": {
51+
"dealerdirect/phpcodesniffer-composer-installer": true
52+
}
2153
}
2254
}

os2forms_rest_api.module

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ use Drupal\os2forms_rest_api\WebformHelper;
1212
* Implements hook_webform_third_party_settings_form_alter().
1313
*
1414
* @see WebformHelper::webformThirdPartySettingsFormAlter()
15+
*
16+
* @phpstan-param array<string, mixed> $form
1517
*/
16-
function os2forms_rest_api_webform_third_party_settings_form_alter(array &$form, FormStateInterface $form_state) {
18+
function os2forms_rest_api_webform_third_party_settings_form_alter(array &$form, FormStateInterface $form_state): void {
1719
\Drupal::service(WebformHelper::class)->webformThirdPartySettingsFormAlter($form, $form_state);
1820
}
1921

2022
/**
2123
* Implements hook_file_download().
24+
*
25+
* @phpstan-return int|array<string, string>|null
2226
*/
2327
function os2forms_rest_api_file_download(string $uri) {
2428
return \Drupal::service(WebformHelper::class)->fileDownload($uri);

phpcs.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<description>The coding standard.</description>
4+
5+
<file>.</file>
6+
7+
<exclude-pattern>vendor/</exclude-pattern>
8+
9+
<!-- Show progress of the run -->
10+
<arg value="p"/>
11+
12+
<arg name="extensions" value="php,module,inc,install,test,profile,theme,css,info,txt,yml"/>
13+
<config name="drupal_core_version" value="9"/>
14+
15+
<rule ref="Drupal">
16+
<!-- <exclude name="Drupal.Files.TxtFileLineLength.TooLong"/> -->
17+
<!-- We want to be able to use "package" and "version" in our custom modules -->
18+
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Project"/>
19+
<exclude name="Drupal.InfoFiles.AutoAddedKeys.Version"/>
20+
</rule>
21+
22+
<rule ref="DrupalPractice"/>
23+
</ruleset>

src/EventSubscriber/EventSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(RouteMatchInterface $routeMatch, AccountProxyInterfa
5050
*
5151
* Check for user access to webform API resource.
5252
*/
53-
public function onRequest(KernelEvent $event) {
53+
public function onRequest(KernelEvent $event): void {
5454
$routeName = $this->routeMatch->getRouteName();
5555
$restRouteNames = [
5656
'rest.webform_rest_elements.GET',

src/WebformHelper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Accou
4949

5050
/**
5151
* Implements hook_webform_third_party_settings_form_alter().
52+
*
53+
* @phpstan-param array<string, mixed> $form
5254
*/
53-
public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterface $form_state) {
55+
public function webformThirdPartySettingsFormAlter(array &$form, FormStateInterface $form_state): void {
5456
/** @var \Drupal\Core\Entity\EntityForm $formObject */
5557
$formObject = $form_state->getFormObject();
5658
/** @var \Drupal\webform\WebformInterface $webform */
@@ -228,6 +230,9 @@ public function getAllowedUsers(WebformInterface $webform): array {
228230

229231
/**
230232
* Load users.
233+
*
234+
* @phpstan-param array<int, mixed> $spec
235+
* @phpstan-return array<int, mixed>
231236
*/
232237
private function loadUsers(array $spec): array {
233238
return $this->entityTypeManager
@@ -237,6 +242,8 @@ private function loadUsers(array $spec): array {
237242

238243
/**
239244
* Implements hook_file_download().
245+
*
246+
* @phpstan-return int|array<string, string>|null
240247
*/
241248
public function fileDownload(string $uri) {
242249
$request = \Drupal::request();

0 commit comments

Comments
 (0)