Skip to content

Commit e2a0abc

Browse files
committed
Refactor UserService, UserPropertiesRepository, and workflows: enhance field mapping error handling, add type assertions, improve compatibility, and update dependencies and documentation for PHP 8.3+.
1 parent 1c71d02 commit e2a0abc

File tree

10 files changed

+175
-42
lines changed

10 files changed

+175
-42
lines changed

.github/workflows/phpunit.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,44 @@ jobs:
1818
strategy:
1919
matrix:
2020
php-version:
21+
- "8.5"
2122
- "8.4"
2223
- "8.3"
23-
- "8.2"
2424

2525
steps:
2626
- uses: actions/checkout@v5
2727
- run: composer install
28-
- run: ./vendor/bin/psalm
29-
- run: ./vendor/bin/phpunit --stderr
28+
- run: composer test
29+
30+
Psalm:
31+
name: Psalm Static Analyzer
32+
runs-on: ubuntu-latest
33+
permissions:
34+
# for github/codeql-action/upload-sarif to upload SARIF results
35+
security-events: write
36+
container:
37+
image: byjg/php:8.4-cli
38+
options: --user root --privileged
39+
40+
steps:
41+
- name: Git checkout
42+
uses: actions/checkout@v4
43+
44+
- name: Composer
45+
run: composer install
46+
47+
- name: Psalm
48+
# Note: Ignoring error code 2, which just signals that some
49+
# flaws were found, not that Psalm itself failed to run.
50+
run: ./vendor/bin/psalm
51+
--show-info=true
52+
--report=psalm-results.sarif || [ $? = 2 ]
53+
54+
- name: Upload Analysis results to GitHub
55+
uses: github/codeql-action/upload-sarif@v4
56+
if: github.ref == 'refs/heads/master'
57+
with:
58+
sarif_file: psalm-results.sarif
3059

3160
Documentation:
3261
if: github.ref == 'refs/heads/master'

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Auth User PHP
1+
# User Authentication
22

3+
[![Sponsor](https://img.shields.io/badge/Sponsor-%23ea4aaa?logo=githubsponsors&logoColor=white&labelColor=0d1117)](https://github.com/sponsors/byjg)
34
[![Build Status](https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml/badge.svg?branch=master)](https://github.com/byjg/php-authuser/actions/workflows/phpunit.yml)
45
[![Opensource ByJG](https://img.shields.io/badge/opensource-byjg-success.svg)](http://opensource.byjg.com)
56
[![GitHub source](https://img.shields.io/badge/Github-source-informational?logo=github)](https://github.com/byjg/php-authuser/)

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "byjg/authuser",
3-
"description": "A simple and customizable class for enable user authentication inside your application. It is available on XML files and Relational Databases.",
3+
"description": "A simple and customizable library for user authentication in PHP applications using a clean repository and service layer architecture.",
44
"autoload": {
55
"psr-4": {
66
"ByJG\\Authenticate\\": "src/"
@@ -14,18 +14,18 @@
1414
"minimum-stability": "dev",
1515
"prefer-stable": true,
1616
"require": {
17-
"php": ">=8.2 <8.5",
17+
"php": ">=8.3 <8.6",
1818
"byjg/micro-orm": "^6.0",
1919
"byjg/cache-engine": "^6.0",
2020
"byjg/jwt-wrapper": "^6.0"
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "^10|^11",
24-
"vimeo/psalm": "^5.9|^6.12"
23+
"phpunit/phpunit": "^10.5|^11.5",
24+
"vimeo/psalm": "^5.9|^6.13"
2525
},
2626
"scripts": {
2727
"test": "vendor/bin/phpunit",
28-
"psalm": "vendor/bin/psalm"
28+
"psalm": "vendor/bin/psalm --threads=1"
2929
},
3030
"license": "MIT"
3131
}

docs/installation.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: Installation
77

88
## Requirements
99

10-
- PHP 8.2 or higher
10+
- PHP 8.3 or higher
1111
- Composer
1212

1313
## Install via Composer
@@ -20,10 +20,8 @@ composer require byjg/authuser
2020

2121
## Running Tests
2222

23-
Because this project uses PHP Session, you need to run the unit tests with the `--stderr` flag:
24-
2523
```bash
26-
./vendor/bin/phpunit --stderr
24+
composer test
2725
```
2826

2927
## Next Steps

psalm.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="4"
3+
errorLevel="3"
44
resolveFromConfigFile="true"
55
findUnusedBaselineEntry="true"
66
findUnusedCode="false"
@@ -11,7 +11,6 @@
1111
>
1212
<projectFiles>
1313
<directory name="src" />
14-
<directory name="tests" />
1514
<ignoreFiles>
1615
<directory name="vendor" />
1716
</ignoreFiles>

src/Definition/PasswordDefinition.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,10 @@ public function generatePassword(int $extendSize = 0): string
185185
$isRepeated = ($char == $previousChar);
186186
$previousChar = strtoupper($previousChar);
187187
$upperChar = strtoupper($char);
188-
$isSequential = ($upperChar == chr(ord($previousChar) + 1)) || ($upperChar == chr(ord($previousChar) - 1));
188+
$prevOrd = ord($previousChar);
189+
$nextOrd = ($prevOrd + 1) % 256;
190+
$prevPrevOrd = ($prevOrd - 1 + 256) % 256;
191+
$isSequential = ($upperChar == chr($nextOrd)) || ($upperChar == chr($prevPrevOrd));
189192
if (!$isRepeated && !$isSequential) {
190193
break;
191194
}

src/MapperFunctions/UserIdGeneratorMapper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public function processedValue(mixed $value, mixed $instance, ?DatabaseExecutor
2121

2222
// Generate from username if instance is UserModel
2323
if ($instance instanceof UserModel) {
24-
return preg_replace('/(?:([\w])|([\W]))/', '\1', strtolower($instance->getUsername()));
24+
$username = $instance->getUsername();
25+
if ($username !== null) {
26+
return preg_replace('/(?:([\w])|([\W]))/', '\1', strtolower($username));
27+
}
2528
}
2629

2730
return $value;

src/Model/UserModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function set(string $name, string|null $value): void
180180
if (empty($property)) {
181181
$property = new UserPropertiesModel($name, $value);
182182
$this->addProperty($property);
183-
} else {
183+
} elseif ($property instanceof UserPropertiesModel) {
184184
$property->setValue($value);
185185
}
186186
}

src/Repository/UserPropertiesRepository.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function save(UserPropertiesModel $model): UserPropertiesModel
7979
public function getByUserId(string|Literal|int $userid): array
8080
{
8181
$userIdMapping = $this->mapper->getFieldMap(UserPropertyField::Userid->value);
82+
if ($userIdMapping === null) {
83+
throw new \InvalidArgumentException('User ID field mapping not found');
84+
}
8285
$userIdField = $userIdMapping->getFieldName();
8386
$query = Query::getInstance()
8487
->table($this->mapper->getTable())
@@ -104,6 +107,10 @@ public function getByUserIdAndName(string|Literal|int $userid, string $propertyN
104107
$userIdMapping = $this->mapper->getFieldMap(UserPropertyField::Userid->value);
105108
$nameMapping = $this->mapper->getFieldMap(UserPropertyField::Name->value);
106109

110+
if ($userIdMapping === null || $nameMapping === null) {
111+
throw new \InvalidArgumentException('Required field mapping not found');
112+
}
113+
107114
$userIdField = $userIdMapping->getFieldName();
108115
$nameField = $nameMapping->getFieldName();
109116

@@ -128,6 +135,9 @@ public function getByUserIdAndName(string|Literal|int $userid, string $propertyN
128135
public function deleteByUserId(string|Literal|int $userid): void
129136
{
130137
$userIdMapping = $this->mapper->getFieldMap(UserPropertyField::Userid->value);
138+
if ($userIdMapping === null) {
139+
throw new \InvalidArgumentException('User ID field mapping not found');
140+
}
131141
$userIdField = $userIdMapping->getFieldName();
132142

133143
$deleteQuery = DeleteQuery::getInstance()
@@ -151,9 +161,16 @@ public function deleteByUserId(string|Literal|int $userid): void
151161
public function deleteByUserIdAndName(string|Literal|int $userid, string $propertyName, ?string $value = null): void
152162
{
153163
$userIdMapping = $this->mapper->getFieldMap(UserPropertyField::Userid->value);
164+
$nameMapping = $this->mapper->getFieldMap(UserPropertyField::Name->value);
165+
$valueMapping = $this->mapper->getFieldMap(UserPropertyField::Value->value);
166+
167+
if ($userIdMapping === null || $nameMapping === null || $valueMapping === null) {
168+
throw new \InvalidArgumentException('Required field mapping not found');
169+
}
170+
154171
$userIdField = $userIdMapping->getFieldName();
155-
$nameField = $this->mapper->getFieldMap(UserPropertyField::Name->value)->getFieldName();
156-
$valueField = $this->mapper->getFieldMap(UserPropertyField::Value->value)->getFieldName();
172+
$nameField = $nameMapping->getFieldName();
173+
$valueField = $valueMapping->getFieldName();
157174

158175
$deleteQuery = DeleteQuery::getInstance()
159176
->table($this->mapper->getTable())
@@ -179,8 +196,15 @@ public function deleteByUserIdAndName(string|Literal|int $userid, string $proper
179196
*/
180197
public function deleteByName(string $propertyName, ?string $value = null): void
181198
{
182-
$nameField = $this->mapper->getFieldMap(UserPropertyField::Name->value)->getFieldName();
183-
$valueField = $this->mapper->getFieldMap(UserPropertyField::Value->value)->getFieldName();
199+
$nameMapping = $this->mapper->getFieldMap(UserPropertyField::Name->value);
200+
$valueMapping = $this->mapper->getFieldMap(UserPropertyField::Value->value);
201+
202+
if ($nameMapping === null || $valueMapping === null) {
203+
throw new \InvalidArgumentException('Required field mapping not found');
204+
}
205+
206+
$nameField = $nameMapping->getFieldName();
207+
$valueField = $valueMapping->getFieldName();
184208

185209
$deleteQuery = DeleteQuery::getInstance()
186210
->table($this->mapper->getTable())

0 commit comments

Comments
 (0)