Skip to content

Commit 91f2b44

Browse files
committed
Update tooling
1 parent fea305b commit 91f2b44

9 files changed

Lines changed: 44 additions & 36 deletions

File tree

.github/workflows/ci.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ jobs:
1313
name: Coding Standard
1414

1515
steps:
16-
- uses: actions/checkout@v3
16+
- uses: actions/checkout@v5
1717
- name: Cache Composer dependencies
18-
uses: actions/cache@v3
18+
uses: actions/cache@v4
1919
with:
2020
path: /tmp/composer-cache
2121
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
@@ -31,7 +31,7 @@ jobs:
3131

3232
steps:
3333
- name: Checkout code
34-
uses: actions/checkout@v4
34+
uses: actions/checkout@v5
3535

3636
- name: Psalm
3737
uses: docker://ghcr.io/psalm/psalm-github-actions
@@ -55,17 +55,17 @@ jobs:
5555
name: "Test: PHP ${{ matrix.php.version }}"
5656

5757
steps:
58-
- uses: actions/checkout@v3
58+
- uses: actions/checkout@v5
5959
- name: Cache Composer dependencies
60-
uses: actions/cache@v3
60+
uses: actions/cache@v4
6161
with:
6262
path: /tmp/composer-cache
6363
key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
6464
- uses: php-actions/composer@v6
6565
with:
6666
dev: no
6767
php_version: "${{ matrix.php.version }}"
68-
- uses: php-actions/phpunit@v3
68+
- uses: php-actions/phpunit@v4
6969
env:
7070
XDEBUG_MODE: coverage
7171
with:

.phive/phars.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phive xmlns="https://phar.io/phive">
3-
<phar name="phpunit" version="^10.0.0" installed="10.5.3" location="./tools/phpunit" copy="true"/>
4-
<phar name="psalm" version="^5.12.0" installed="5.17.0" location="./tools/psalm" copy="true"/>
3+
<phar name="phpunit" version="^12.0.0" installed="12.4.2" location="./tools/phpunit" copy="true"/>
4+
<phar name="psalm" version="^6.0.0" installed="6.13.1" location="./tools/psalm" copy="true"/>
55
<phar name="phpcpd" version="^6.0.0" installed="6.0.3" location="./tools/phpcpd" copy="true"/>
6-
<phar name="composer-require-checker" version="^4.2.0" installed="4.8.0" location="./tools/composer-require-checker" copy="true"/>
7-
<phar name="phploc" version="^7.0.0" installed="7.0.2" location="./tools/phploc" copy="true"/>
86
</phive>

bin/json-schema-validator

100644100755
45.5 KB
Binary file not shown.

box.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
],
88
"force-autodiscovery": true,
99
"algorithm": "SHA256",
10-
"banner": "JSON Schema Validator by Andreas Wahlen",
10+
"banner": "JSON Schema Validator by nerou GmbH",
1111
"git": "git-version"
1212
}

composer.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nerou/json-schema-validator",
3-
"version": "1.0.3",
3+
"description": "Validate JSON Schema files against the JSON Schema Standard",
44
"type": "library",
55
"license": "MIT",
66
"authors": [
@@ -15,10 +15,11 @@
1515
"php": ">=8.0",
1616
"ext-json": "*",
1717
"opis/json-schema": "^2.3",
18-
"nerou/cli-parser": "^0.1.0"
18+
"nerou/cli-parser": "^0.1"
1919
},
2020
"require-dev": {
21-
"squizlabs/php_codesniffer": ">=3.7"
21+
"squizlabs/php_codesniffer": "^4.0",
22+
"humbug/box": "^4.6"
2223
},
2324
"prefer-stable": true,
2425
"autoload": {
@@ -35,10 +36,7 @@
3536
"phpunit": "./tools/phpunit test",
3637
"phpcpd": "./tools/phpcpd src",
3738
"psalm": "./tools/psalm --no-diff --use-baseline=psalm.baseline.xml --php-version=8.1",
38-
"psalm-stats": "./tools/psalm --no-diff --use-baseline=psalm.baseline.xml --php-version=8.1 --stats | grep -v 100",
3939
"update-psalm-baseline": "./tools/psalm --no-diff --set-baseline=psalm.baseline.xml",
40-
"require-checker": "./tools/composer-require-checker check",
41-
"lines": "./tools/phploc src",
4240
"build": "./vendor/bin/box compile",
4341
"tests": [
4442
"@phpcs",

phpcs.xml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
<exclude name="Generic.Files.LowercasedFilename"/>
2727
<exclude name="Generic.Formatting.DisallowMultipleStatements"/>
2828
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/>
29-
<exclude name="Generic.Formatting.NoSpaceAfterCast"/>
29+
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSame"/>
30+
<exclude name="Generic.Formatting.SpaceAfterCast"/> <!-- see below -->
3031
<exclude name="Generic.Formatting.SpaceBeforeCast"/>
3132
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman"/>
3233
<exclude name="Generic.Metrics.CyclomaticComplexity"/> <!-- see below -->
@@ -39,10 +40,6 @@
3940
<exclude name="Generic.Strings.UnnecessaryStringConcat"/>
4041
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/>
4142
<exclude name="Generic.WhiteSpace.ScopeIndent"/> <!-- see below -->
42-
43-
<!-- temporary -->
44-
<exclude name="Generic.CodeAnalysis.UnusedFunctionParameter"/>
45-
<exclude name="Generic.Metrics.NestingLevel"/>
4643
</rule>
4744
<rule ref="Generic.Arrays.ArrayIndent">
4845
<properties>
@@ -56,6 +53,11 @@
5653
<rule ref="Generic.Commenting">
5754
<type>warning</type>
5855
</rule>
56+
<rule ref="Generic.Formatting.SpaceAfterCast">
57+
<properties>
58+
<property name="spacing" value="0"/>
59+
</properties>
60+
</rule>
5961
<rule ref="Generic.Formatting.SpaceAfterNot">
6062
<properties>
6163
<property name="spacing" value="0"/>
@@ -104,7 +106,6 @@
104106
<rule ref="Squiz.Strings.DoubleQuoteUsage"/>
105107
<rule ref="Squiz.Strings.EchoedStrings"/>
106108
<rule ref="Squiz.WhiteSpace.CastSpacing"/>
107-
<rule ref="Squiz.WhiteSpace.LanguageConstructSpacing"/>
108109
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>
109110
<rule ref="Squiz.WhiteSpace.SemicolonSpacing"/>
110111
</ruleset>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
autoloader="vendor/autoload.php"
66
allowStringToStandInForClass="true"
77
errorBaseline="psalm.baseline.xml"
8-
errorLevel="2"
8+
errorLevel="1"
99
findUnusedBaselineEntry="false"
1010
findUnusedCode="false">
1111

src/functions.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ function collectSchemaFiles(array $fileList): array {
2929
/**
3030
* @return string[]
3131
* @psalm-return list<string>
32+
* @throws \InvalidArgumentException if given folder is not accessible
3233
*/
3334
function listSchemasInFolder(string $folder): array {
3435
$folder = str_ends_with($folder, '/') ? substr($folder, 0, strlen($folder) - 1) : $folder;
36+
$scanRes = scandir($folder);
37+
if($scanRes === false){
38+
throw new \InvalidArgumentException('folder '.$folder.' does not exist or is not readable');
39+
}
3540
$res = [];
36-
foreach(scandir($folder) as $filename){
41+
foreach($scanRes as $filename){
3742
if(is_file($folder.'/'.$filename) && is_readable($folder.'/'.$filename) && str_ends_with($filename, '.json') !== false){
3843
$res[] = $folder.'/'.$filename;
3944
} else if(is_dir($folder.'/'.$filename) && is_readable($folder.'/'.$filename) && $filename !== '.' && $filename !== '..'){

src/validateJSONSchema.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
exit(1); // exit if not run via CLI
1717
}
1818

19-
echo 'JSON Schema Validator @git-version@ by Andreas Wahlen'.PHP_EOL.PHP_EOL;
20-
21-
if(!class_exists(\Opis\JsonSchema\Validator::class)){
22-
echo "\033[31mFATAL: Opis\JsonSchema\Validator not found, is it required in your composer.json?\033[0m".PHP_EOL.PHP_EOL;
23-
exit(10);
24-
}
19+
echo 'JSON Schema Validator @git-version@ by nerou GmbH'.PHP_EOL.PHP_EOL;
2520

2621
$cliArgs = new CLIParser($_SERVER['argv']);
2722
$cliArgs->setAllowedOptions([]);
@@ -36,12 +31,15 @@
3631
// collect schema files to validate
3732
try {
3833
$files = collectSchemaFiles($cliArgs->getCommands());
39-
} catch(InvalidArgumentException $ex){
34+
} catch(\InvalidArgumentException $ex){
4035
echo "\033[31m".$ex->getMessage()."\033[0m".PHP_EOL;
4136
exit(20);
4237
}
4338

4439
$cwd = getcwd();
40+
if($cwd === false){
41+
throw new \UnexpectedValueException('unable to get current working directory');
42+
}
4543
$validator = new Validator();
4644
$validator->resolver()->registerPrefix('https://json-schema.org/', dirname(__DIR__).'/resource');
4745
$validator->resolver()->registerPrefix('http://json-schema.org/', dirname(__DIR__).'/resource');
@@ -52,10 +50,14 @@
5250
echo '['.str_pad(strval($index+1), $countChars, ' ', STR_PAD_LEFT).'/'.count($files).'] '.
5351
$formattedFilename;
5452
try {
53+
$fileContent = file_get_contents($file);
54+
if($fileContent === false){
55+
throw new \UnexpectedValueException('file '.$file.' could not be read');
56+
}
5557
/**
5658
* @var scalar|object $json
5759
*/
58-
$json = json_decode(file_get_contents($file), flags: JSON_THROW_ON_ERROR);
60+
$json = json_decode($fileContent, flags: JSON_THROW_ON_ERROR);
5961
} catch(JsonException $ex){
6062
echo "[\033[31mSYNTAX ERROR\033[0m]".PHP_EOL;
6163
$errors++;
@@ -65,8 +67,10 @@
6567
try {
6668
/**
6769
* @psalm-suppress PossiblyInvalidPropertyFetch
70+
* @var string $schema
6871
*/
69-
$result = $validator->validate($json, $json->{'$schema'} ?? 'https://json-schema.org/draft/2020-12/schema');
72+
$schema = $json->{'$schema'} ?? 'https://json-schema.org/draft/2020-12/schema';
73+
$result = $validator->validate($json, $schema);
7074
if(!$result->isValid()){
7175
$errorMessage = json_encode(((new ErrorFormatter())->format($result->error())),
7276
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
@@ -86,8 +90,10 @@
8690
}
8791
}
8892
}
89-
echo 'consumed '.number_format(microtime(true) - $startTime, 3).' s of processing time and '.
90-
(memory_get_peak_usage(true) / 1024 / 1024).' MiB of memory'.PHP_EOL.PHP_EOL;
93+
echo sprintf('Time: %.3s s, Memory: %.2f MiB'.PHP_EOL.PHP_EOL,
94+
microtime(true) - $startTime,
95+
((float) memory_get_peak_usage(true)) / 1024. / 1024.
96+
);
9197
echo 'JSON Schemas: '.count($files).', errors: '.$errors.PHP_EOL;
9298
if($errors > 0){
9399
exit(30);

0 commit comments

Comments
 (0)