Skip to content

Commit 6c9ef3d

Browse files
committed
Align with Contributte library standards
- Use contributte/tester ^0.3.0 and contributte/phpstan ^0.2.0 - Update phpstan.neon with phpVersion and scanDirectories - Update Makefile with GITHUB_ACTION conditional logic for CI/CD - Use reusable workflows from contributte/.github - Update tests to use Contributte\Tester\Environment - Fix deprecated Authenticator constants (use new PascalCase names) - Update ruleset.xml with modern array syntax - Add test descriptions per new Nette Tester requirements
1 parent 3351f3a commit 6c9ef3d

File tree

13 files changed

+93
-163
lines changed

13 files changed

+93
-163
lines changed

.github/workflows/codesniffer.yml

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
name: Code Sniffer
1+
name: Codesniffer
22

33
on:
4-
push:
5-
branches:
6-
- "master"
74
pull_request:
8-
types:
9-
- opened
10-
- synchronize
11-
- reopened
125
workflow_dispatch:
6+
push:
7+
branches:
8+
- "**"
9+
schedule:
10+
- cron: "0 10 * * 1"
1311

1412
jobs:
1513
codesniffer:
16-
name: Code Sniffer
17-
runs-on: ubuntu-latest
18-
19-
steps:
20-
- name: Checkout
21-
uses: actions/checkout@v4
22-
23-
- name: Setup PHP
24-
uses: shivammathur/setup-php@v2
25-
with:
26-
php-version: 8.2
27-
extensions: json
28-
tools: composer
29-
30-
- name: Install dependencies
31-
run: composer update --no-interaction --no-progress --prefer-dist --prefer-stable
32-
33-
- name: Run Code Sniffer
34-
run: make cs
14+
name: "Codesniffer"
15+
uses: contributte/.github/.github/workflows/codesniffer.yml@master
16+
with:
17+
php: 8.2

.github/workflows/coverage.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

.github/workflows/phpstan.yml

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
11
name: PHPStan
22

33
on:
4-
push:
5-
branches:
6-
- "master"
74
pull_request:
8-
types:
9-
- opened
10-
- synchronize
11-
- reopened
125
workflow_dispatch:
6+
push:
7+
branches:
8+
- "**"
9+
schedule:
10+
- cron: "0 10 * * 1"
1311

1412
jobs:
1513
phpstan:
16-
name: PHPStan
17-
runs-on: ubuntu-latest
18-
19-
steps:
20-
- name: Checkout
21-
uses: actions/checkout@v4
22-
23-
- name: Setup PHP
24-
uses: shivammathur/setup-php@v2
25-
with:
26-
php-version: 8.2
27-
extensions: json
28-
tools: composer
29-
30-
- name: Install dependencies
31-
run: composer update --no-interaction --no-progress --prefer-dist --prefer-stable
32-
33-
- name: Run PHPStan
34-
run: make phpstan
14+
name: "PHPStan"
15+
uses: contributte/.github/.github/workflows/phpstan.yml@master
16+
with:
17+
php: 8.2

.github/workflows/tests.yml

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,36 @@
11
name: Tests
22

33
on:
4+
pull_request:
5+
workflow_dispatch:
46
push:
57
branches:
6-
- "master"
7-
pull_request:
8-
types:
9-
- opened
10-
- synchronize
11-
- reopened
8+
- "**"
129
schedule:
13-
- cron: "0 8 * * 1"
14-
workflow_dispatch:
10+
- cron: "0 10 * * 1"
1511

1612
jobs:
17-
tests:
18-
name: Tests
19-
runs-on: ubuntu-latest
20-
strategy:
21-
fail-fast: false
22-
matrix:
23-
php-version:
24-
- 8.2
25-
- 8.3
26-
- 8.4
27-
composer-args:
28-
- ""
29-
include:
30-
- php-version: 8.2
31-
composer-args: "--prefer-lowest"
32-
33-
steps:
34-
- name: Checkout
35-
uses: actions/checkout@v4
36-
37-
- name: Setup PHP
38-
uses: shivammathur/setup-php@v2
39-
with:
40-
php-version: ${{ matrix.php-version }}
41-
extensions: json
42-
tools: composer
13+
test84:
14+
name: "Tests PHP 8.4"
15+
uses: contributte/.github/.github/workflows/nette-tester.yml@master
16+
with:
17+
php: 8.4
4318

44-
- name: Validate Composer
45-
run: composer validate --strict
19+
test83:
20+
name: "Tests PHP 8.3"
21+
uses: contributte/.github/.github/workflows/nette-tester.yml@master
22+
with:
23+
php: 8.3
4624

47-
- name: Install dependencies
48-
run: composer update --no-interaction --no-progress --prefer-dist --prefer-stable ${{ matrix.composer-args }}
25+
test82:
26+
name: "Tests PHP 8.2"
27+
uses: contributte/.github/.github/workflows/nette-tester.yml@master
28+
with:
29+
php: 8.2
4930

50-
- name: Run tests
51-
run: make tests
31+
testlowest:
32+
name: "Tests PHP 8.2 Lowest"
33+
uses: contributte/.github/.github/workflows/nette-tester.yml@master
34+
with:
35+
php: 8.2
36+
composer: "composer update --no-interaction --no-progress --prefer-dist --prefer-stable --prefer-lowest"

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ install:
66
qa: phpstan cs
77

88
cs:
9+
ifdef GITHUB_ACTION
10+
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 -q --report=checkstyle src tests | cs2pr
11+
else
912
vendor/bin/phpcs --standard=ruleset.xml --encoding=utf-8 -nsp src tests
13+
endif
1014

1115
csf:
1216
vendor/bin/phpcbf --standard=ruleset.xml --encoding=utf-8 -nsp src tests
@@ -18,4 +22,8 @@ tests:
1822
vendor/bin/tester -s -p php --colors 1 -C tests/Cases
1923

2024
coverage:
25+
ifdef GITHUB_ACTION
2126
vendor/bin/tester -s -p php --colors 1 -C --coverage ./coverage.xml --coverage-src ./src tests/Cases
27+
else
28+
vendor/bin/tester -s -p php --colors 1 -C --coverage ./coverage.html --coverage-src ./src tests/Cases
29+
endif

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"homepage": "https://github.com/contributte/security",
1515
"authors": [
1616
{
17-
"name": "Milan Felix Sulc",
17+
"name": "Milan Felix Šulc",
1818
"homepage": "https://f3l1x.io"
1919
}
2020
],
@@ -23,10 +23,10 @@
2323
"nette/security": "^3.2"
2424
},
2525
"require-dev": {
26-
"contributte/qa": "^0.4",
27-
"contributte/phpstan": "^0.1",
28-
"nette/tester": "^2.5",
29-
"mockery/mockery": "^1.6"
26+
"contributte/phpstan": "^0.2.0",
27+
"contributte/qa": "^0.4.0",
28+
"contributte/tester": "^0.3.0",
29+
"mockery/mockery": "^1.6.12"
3030
},
3131
"suggest": {
3232
"nette/di": "to use SecurityExtension [CompilerExtension]"
@@ -38,7 +38,7 @@
3838
},
3939
"autoload-dev": {
4040
"psr-4": {
41-
"Tests\\Contributte\\Security\\": "tests"
41+
"Tests\\": "tests"
4242
}
4343
},
4444
"minimum-stability": "dev",

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ includes:
33

44
parameters:
55
level: 9
6+
phpVersion: 80200
7+
scanDirectories:
8+
- src
9+
fileExtensions:
10+
- php
611
paths:
712
- src
813
- .docs

ruleset.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?xml version="1.0"?>
2-
<ruleset>
2+
<ruleset name="Contributte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
33
<!-- Contributte Coding Standard -->
44
<rule ref="./vendor/contributte/qa/ruleset-8.2.xml"/>
55

66
<!-- Specific rules -->
77
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
88
<properties>
9-
<property name="rootNamespaces" type="array" value="
10-
src=>Contributte\Security,
11-
tests=>Tests\Contributte\Security
12-
"/>
9+
<property name="rootNamespaces" type="array">
10+
<element key="src" value="Contributte\Security"/>
11+
<element key="tests" value="Tests"/>
12+
</property>
1313
</properties>
1414
</rule>
1515

src/Auth/DebugAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function setIdentity(IIdentity $identity): void
2626
public function authenticate(string $username, string $password): IIdentity
2727
{
2828
if ($this->pass === false) {
29-
throw new AuthenticationException('Cannot login', Authenticator::FAILURE);
29+
throw new AuthenticationException('Cannot login', Authenticator::Failure);
3030
}
3131

3232
if ($this->identity !== null) {

src/Auth/StaticAuthenticator.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,13 @@ public function __construct(
2828
throw new InvalidArgumentException(sprintf('Missing parameter `password` for user `%s`', $username));
2929
}
3030

31+
if (!is_string($values['password'])) {
32+
throw new InvalidArgumentException(sprintf('Password for user `%s` must be a string', $username));
33+
}
34+
3135
$this->list[$username] = [
3236
'password' => $values['password'],
33-
'unsecured' => $values['unsecured'] ?? false,
37+
'unsecured' => (bool) ($values['unsecured'] ?? false),
3438
'identity' => $this->createIdentity($username, $values),
3539
];
3640
}
@@ -39,7 +43,7 @@ public function __construct(
3943
public function authenticate(string $username, string $password): IIdentity
4044
{
4145
if (!isset($this->list[$username])) {
42-
throw new AuthenticationException(sprintf('User `%s` not found', $username), Authenticator::IDENTITY_NOT_FOUND);
46+
throw new AuthenticationException(sprintf('User `%s` not found', $username), Authenticator::IdentityNotFound);
4347
}
4448

4549
$user = $this->list[$username];
@@ -48,7 +52,7 @@ public function authenticate(string $username, string $password): IIdentity
4852
($user['unsecured'] === true && !hash_equals($password, $user['password'])) ||
4953
($user['unsecured'] === false && !$this->passwords->verify($password, $user['password']))
5054
) {
51-
throw new AuthenticationException('Invalid password', Authenticator::INVALID_CREDENTIAL);
55+
throw new AuthenticationException('Invalid password', Authenticator::InvalidCredential);
5256
}
5357

5458
return $user['identity'];
@@ -69,11 +73,11 @@ private function createIdentity(string $username, array $values): IIdentity
6973
return $identity;
7074
}
7175

72-
if (is_array($values['identity'])) {
76+
if (is_array($identity)) {
7377
return new SimpleIdentity(
74-
$identity['id'] ?? $username,
75-
$identity['roles'] ?? null,
76-
$identity['date'] ?? null
78+
isset($identity['id']) && (is_string($identity['id']) || is_int($identity['id'])) ? $identity['id'] : $username,
79+
isset($identity['roles']) && is_array($identity['roles']) ? $identity['roles'] : null,
80+
isset($identity['data']) && is_array($identity['data']) ? $identity['data'] : null
7781
);
7882
}
7983

0 commit comments

Comments
 (0)