Skip to content

Commit

Permalink
Opis JSON Schema validation for PHP 7.0 (#76)
Browse files Browse the repository at this point in the history
This PR adds a PHP 7.0-compatible a version of the
[Opis](https://opis.io/json-schema/2.x/) JSON Schema validator.

Opis seems to be the most complete and well-maintained JSON Schema
validator for PHP. However,
Blueprints aim for PHP 7.0+ support and Opis is not compatible with PHP
7.0. Therefore, it was
transpiled to PHP 7.0 with [rector](https://github.com/rectorphp/rector)
and provided here.

See #75 for more
context.

cc @reimic
  • Loading branch information
adamziel authored Mar 19, 2024
1 parent a9f10c1 commit 07a0068
Show file tree
Hide file tree
Showing 230 changed files with 28,265 additions and 1,167 deletions.
11 changes: 3 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- trunk
- trunk
pull_request:

jobs:
Expand All @@ -25,15 +25,10 @@ jobs:
- name: Install Dependencies
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: Check PHP version compatibility
uses: pantheon-systems/phpcompatibility-action@v1
with:
skip-php-setup: true
test-versions: 7.4-
paths: ${{ github.workspace }}/src
- name: PHPCS checks
run: ./vendor/bin/phpcs ${{ github.workspace }}/src

test-unit:
needs: [php-cs-check]
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
Expand Down
68 changes: 65 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,30 @@ vendor/bin/phpunit --testdox
### Regenerate models files from JSON schema with

```shell
php src/WordPress/Blueprints/bin/autogenerate_models.php
composer global require jane-php/json-schema
php src/WordPress/Blueprints/bin/autogenerate_models.php
```

## Building to .phar

The Blueprints library is distributed as a .phar library. To build the .phar file, run:
The Blueprints library is distributed as a .phar library. To build the .phar file, install box:

```shell
vendor/bin/box compile
composer global require humbug/box
```

And then run:

```shell
rm composer.lock
rm -rf vendor
COMPOSER=composer-web.json composer install --no-dev
rm -rf vendor/pimple/pimple/ext/
rm -rf vendor/symfony/*/*.md
rm -rf vendor/symfony/*/composer.json
rm -rf vendor/symfony/*/*.dist
rm -rf vendor/*/*/LICENSE
box compile
```

Note that in box.json, the `"check-requirements"` option is set to `false`. Somehow, keeping it as `true` results in a
Expand All @@ -112,6 +127,53 @@ To try the built .phar file, run:
rm -rf new-wp/* && USE_PHAR=1 php blueprint_compiling.php
```

## PHP 7.0 Compatibility

This project is compatible with PHP >= 7.0.

Part of the process is automated with [rector](https://github.com/rectorphp/rector),
which transpiles the features added in PHP 7.2 and later to PHP 7.1. Unfortunately,
that's as far as Rector goes.

From there, manual transformations are required to bring the compatibility further down to PHP 7.0.

### Automated part

To transpile the code to PHP 7.1, run:

```bash
# Install rector:
composer require rector/rector --ignore-platform-req=php

# Transpile:
php vendor/bin/rector process src
```

Unfortunately, Rector does not support downgrading to PHP 7.0 yet, so we need to do the
last stretch manually.

### Manual part

Rector will downgrade PHP code to PHP 7.1 but not further. We need PHP 7.0 compat
so here's a few additional regexps to run. Regexps are not, of course, reliable in
the general case, but they seem to do the trick here.

List of manual replacements

* `: \?[a-zA-Z_0-9]+` -> (empty string) to remove the unsupported return type
from `function(): ?SchemaResolver {}` -> `function() {}`.
* `: iterable` to fix `Fatal error: Generators may only declare a return type of Generator, Iterator or Traversable`.
* `\?[a-zA-Z_0-9]+ \$` -> `$` to remove the unsupported nullable type from function signatures,
e.g. `function(?Schema $schema){}` -> `function($schema){}`.
* `(protected|public|private) const` -> `const` as const visibility is not supported in PHP 7.0.
* `: void` -> `` as `void` return type is unsupported in PHP 7.0.

@TODO:

* `[$ns, $name] = $this->parseName($name);` -> `list($ns, $name) = $this->parseName($name);`
* `foreach ($data as [$cp, $chars]) {` -> `foreach ($data as list($cp, $chars)) {`
* Find or write Rector rules for downgrading to PHP 7.0

## License

WordPress Blueprints are open-source software licensed under the GPL.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"symfony/process": "*",
"pimple/pimple": "*",
"psr/simple-cache": "*",
"opis/json-schema": "*",
"ext-json": "*",
"nikic/php-parser": "v4.18.0"
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "*",
Expand Down Expand Up @@ -36,7 +34,10 @@
],
"psr-4": {
"WordPress\\": "src/WordPress",
"Symfony\\Component\\Process\\": "vendor/symfony/process"
"Symfony\\Component\\Process\\": "vendor/symfony/process",
"Opis\\JsonSchema\\": "src/opis/json-schema/src",
"Opis\\String\\": "src/opis/string/src",
"Opis\\Uri\\": "src/opis/uri/src"
},
"files": [
"src/WordPress/Blueprints/functions.php",
Expand Down
Loading

0 comments on commit 07a0068

Please sign in to comment.