Skip to content

Commit e348ebd

Browse files
author
Greg Bowler
authored
feature: implement asArray on Input (#284)
* feature: implement asArray on Input closes #283 * wip: typed function tests for working with arrays * maintenance: harden type system * maintenance: improve after static analysis * ci: upgrade to matrix runner * build: phpcs and phpmd * test: action codesniffer tests * fix: inherit return type * fix: action mess detector tests * docs: update badges * test: check multiple float usage
1 parent fbcd560 commit e348ebd

40 files changed

+1753
-509
lines changed

.github/workflows/ci.yml

+109-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
name: CI
22

3-
on: [push, pull_request]
3+
on: [push]
44

55
jobs:
66
composer:
77
runs-on: ubuntu-latest
8+
strategy:
9+
matrix:
10+
php: [ 8.1, 8.2 ]
811

912
steps:
1013
- uses: actions/checkout@v3
@@ -15,10 +18,10 @@ jobs:
1518
path: /tmp/composer-cache
1619
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
1720

18-
- name: Composer
21+
- name: Composer install
1922
uses: php-actions/composer@v6
2023
with:
21-
php_version: '8.1'
24+
php_version: ${{ matrix.php }}
2225

2326
- name: Archive build
2427
run: mkdir /tmp/github-actions/ && tar -cvf /tmp/github-actions/build.tar ./
@@ -31,7 +34,13 @@ jobs:
3134

3235
phpunit:
3336
runs-on: ubuntu-latest
34-
needs: [composer]
37+
needs: [ composer ]
38+
strategy:
39+
matrix:
40+
php: [ 8.1, 8.2 ]
41+
42+
outputs:
43+
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
3544

3645
steps:
3746
- uses: actions/download-artifact@v3
@@ -44,15 +53,46 @@ jobs:
4453

4554
- name: PHP Unit tests
4655
uses: php-actions/phpunit@v3
56+
env:
57+
XDEBUG_MODE: cover
4758
with:
48-
php_version: 8.1
59+
php_version: ${{ matrix.php }}
4960
php_extensions: xdebug
5061
configuration: test/phpunit/phpunit.xml
5162
bootstrap: vendor/autoload.php
63+
coverage_text: _coverage/coverage.txt
64+
coverage_clover: _coverage/clover.xml
65+
66+
- name: Store coverage data
67+
uses: actions/upload-artifact@v3
68+
with:
69+
name: code-coverage
70+
path: _coverage
71+
72+
coverage:
73+
runs-on: ubuntu-latest
74+
needs: [ phpunit ]
75+
76+
steps:
77+
- uses: actions/checkout@v3
78+
79+
- uses: actions/download-artifact@v3
80+
with:
81+
name: code-coverage
82+
path: _coverage
83+
84+
- name: Output coverage
85+
run: cat "_coverage/coverage.txt"
86+
87+
- name: Upload to Codecov
88+
uses: codecov/codecov-action@v3
5289

5390
phpstan:
5491
runs-on: ubuntu-latest
55-
needs: [composer]
92+
needs: [ composer ]
93+
strategy:
94+
matrix:
95+
php: [ 8.1, 8.2 ]
5696

5797
steps:
5898
- uses: actions/download-artifact@v3
@@ -66,4 +106,67 @@ jobs:
66106
- name: PHP Static Analysis
67107
uses: php-actions/phpstan@v3
68108
with:
109+
php_version: ${{ matrix.php }}
110+
path: src/
111+
112+
phpmd:
113+
runs-on: ubuntu-latest
114+
needs: [ composer ]
115+
strategy:
116+
matrix:
117+
php: [ 8.1, 8.2 ]
118+
119+
steps:
120+
- uses: actions/download-artifact@v3
121+
with:
122+
name: build-artifact
123+
path: /tmp/github-actions
124+
125+
- name: Extract build archive
126+
run: tar -xvf /tmp/github-actions/build.tar ./
127+
128+
- name: PHP Mess Detector
129+
uses: php-actions/phpmd@v1
130+
with:
131+
php_version: ${{ matrix.php }}
69132
path: src/
133+
output: text
134+
ruleset: phpmd.xml
135+
136+
phpcs:
137+
runs-on: ubuntu-latest
138+
needs: [ composer ]
139+
strategy:
140+
matrix:
141+
php: [ 8.1, 8.2 ]
142+
143+
steps:
144+
- uses: actions/download-artifact@v3
145+
with:
146+
name: build-artifact
147+
path: /tmp/github-actions
148+
149+
- name: Extract build archive
150+
run: tar -xvf /tmp/github-actions/build.tar ./
151+
152+
- name: PHP Code Sniffer
153+
uses: php-actions/phpcs@v1
154+
with:
155+
php_version: ${{ matrix.php }}
156+
path: src/
157+
standard: phpcs.xml
158+
159+
remove_old_artifacts:
160+
runs-on: ubuntu-latest
161+
162+
steps:
163+
- name: Remove old artifacts for prior workflow runs on this repository
164+
env:
165+
GH_TOKEN: ${{ github.token }}
166+
run: |
167+
gh api "/repos/${{ github.repository }}/actions/artifacts?name=build-artifact" | jq ".artifacts[] | select(.name == \"build-artifact\") | .id" > artifact-id-list.txt
168+
while read id
169+
do
170+
echo -n "Deleting artifact ID $id ... "
171+
gh api --method DELETE /repos/${{ github.repository }}/actions/artifacts/$id && echo "Done"
172+
done <artifact-id-list.txt

.scrutinizer.yml

-32
This file was deleted.

README.md

+21-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
Encapsulated and type-safe user input.
2-
====================================
2+
======================================
33

44
By default, PHP stores all user input in global arrays (`$_GET`, `$_POST`, and `$_FILES`) available for reading and _modification_ in any code, including third party libraries.
55

66
This library wraps user input in objects that promote encapsulation, allowing functions to be
77
passed only the user input they require, rather than having unmitigated read/write access to everything.
88

9-
Type-safe functions allow more predictable functionality, such as `$input->getFileUpload("photo")` and `$input->getDateTime("date-of-birth")`.
9+
Type-safe functions allow more predictable functionality, such as `$input->getFileUpload("photo")`, `$input->getDateTime("date-of-birth")`, and `$input->getMultipleString("pizza-topping")`.
1010

1111
***
1212

1313
<a href="https://github.com/PhpGt/Input/actions" target="_blank">
1414
<img src="https://badge.status.php.gt/input-build.svg" alt="PHP.Gt/Input build status" />
1515
</a>
16-
<a href="https://scrutinizer-ci.com/g/phpgt/input" target="_blank">
16+
<a href="https://app.codacy.com/gh/PhpGt/Input" target="_blank">
1717
<img src="https://badge.status.php.gt/input-quality.svg" alt="PHP.Gt/Input code quality" />
1818
</a>
19-
<a href="https://scrutinizer-ci.com/g/phpgt/input" target="_blank">
19+
<a href="https://app.codecov.io/gh/PhpGt/Input" target="_blank">
2020
<img src="https://badge.status.php.gt/input-coverage.svg" alt="PHP.Gt/Input code coverage" />
2121
</a>
2222
<a href="https://packagist.org/packages/phpgt/input" target="_blank">
@@ -36,10 +36,20 @@ Example usage
3636
<span>Your name</span>
3737
<input name="name" placeholder="e.g. Eugene Kaspersky" required />
3838
</label>
39-
39+
4040
<label>
4141
<span>Age</span>
42-
<input name="age" type="number" required />
42+
<input type="number" name="age" />
43+
</label>
44+
45+
<label>
46+
<span>Interests</span>
47+
<select name="interest[]" multiple>
48+
<option>Mathematics</option>
49+
<option>Cryptography</option>
50+
<option>Information Security</option>
51+
<option>Cyberwarfare</option>
52+
</select>
4353
</label>
4454

4555
<label>
@@ -60,6 +70,11 @@ $profile->update(
6070
$input->getInt("age"),
6171
);
6272

73+
// Handle multiple values with type safety.
74+
foreach($input->getMultipleString("interest") as $interest) {
75+
$profile->addInterest($interest);
76+
}
77+
6378
// Handle file uploads with a FileUpload object.
6479
$photoUpload = $input->getFile("photo");
6580
if($photoUpload instanceof FailedFileUpload) {

composer.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
"require": {
77
"php": ">=8.1",
8-
"phpgt/http": "^v1.1"
8+
"phpgt/http": "^1.1"
99
},
1010

1111
"require-dev": {
12-
"phpstan/phpstan": "^v1.8",
13-
"phpunit/phpunit": "^v9.5"
12+
"phpstan/phpstan": "^1.10",
13+
"phpunit/phpunit": "^10.0",
14+
"phpmd/phpmd": "^2.13",
15+
"squizlabs/php_codesniffer": "^3.7"
1416
},
1517

1618
"autoload": {

0 commit comments

Comments
 (0)