Skip to content

[Help/Contributor needed] Major release & refactoring for PHP8 support #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ cache:

matrix:
include:
- php: 5.3
- php: 5.4
- php: 5.5
- php: 5.6
- php: hhvm
- php: nightly
- php: 7.0
- php: 7.3
env: COVERAGE=yes
- php: 7.0
- php: 7.3
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
- php: 7.4
env: COVERAGE=yes
- php: 7.4
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
- php: 8.0
env: COVERAGE=yes
- php: 8.0
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
allow_failures:
- php: hhvm
- php: nightly
fast_finish: true

before_install:
- if [[ $TRAVIS_PHP_VERSION != hhvm && $COVERAGE != yes ]]; then phpenv config-rm xdebug.ini; fi;
- if [[ $COVERAGE != yes ]]; then phpenv config-rm xdebug.ini; fi;
- if [[ $TRAVIS_REPO_SLUG = webmozart/json ]]; then cp .composer-auth.json ~/.composer/auth.json; fi;
- composer self-update

Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Changelog
=========

* 2.0.0 (@release_date@)

* dropped support for PHP ^5.2
* dropped support for PHP 7.0
* dropped support for PHP 7.1
* dropped support for PHP 7.2

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think breaking backward-compatibility in that regard is something reasonable.

The latest release is 1.2.2 in 2016, supporting the minimum PHP version 5.3. This means at least 8 years of BC. Your PR is trying to cut it down into 3 years, which IMO is not good.

Don't get me wrong, I don't say having 3-year compatibility is a bad idea. I mean, if we have something with 8-year compatibility and you want to make it 3, we should do it in many small steps, instead of one big step.

I think upgrading the minimum PHP version to 5.6 is a good idea. Or at the very least, we can make it 7.1.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why support a version of PHP not supported by PHP itself? The minimum supported version should be PHP 7.4. Otherwise you're talking about supporting PHP versions that have been EOL for years, and this is going to cause dependency hell as other packages drop support.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huge jumps could lag users behind. So instead of jumping to 7.4 in a sudden, we could move to 5.6, 7.2 and then 7.4, for example.

* upgraded dependencies
* added PHP 8 compatibility

* 1.3.0 (@release_date@)

* added `JsonConverter` and `ConversionException`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Webmozart JSON
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/webmozart/json/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/webmozart/json/?branch=master)
[![Latest Stable Version](https://poser.pugx.org/webmozart/json/v/stable.svg)](https://packagist.org/packages/webmozart/json)
[![Total Downloads](https://poser.pugx.org/webmozart/json/downloads.svg)](https://packagist.org/packages/webmozart/json)
[![Dependency Status](https://www.versioneye.com/php/webmozart:json/1.2.2/badge.svg)](https://www.versioneye.com/php/webmozart:json/1.2.2)
[![Dependency Status](https://www.versioneye.com/php/webmozart:json/2.0.0/badge.svg)](https://www.versioneye.com/php/webmozart:json/2.0.0)

Latest release: [1.2.2](https://packagist.org/packages/webmozart/json#1.2.2)
Latest release: [2.0.0](https://packagist.org/packages/webmozart/json#2.0.0)

A robust wrapper for `json_encode()`/`json_decode()` that normalizes their
behavior across PHP versions, throws meaningful exceptions and supports schema
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
}
],
"require": {
"php": "^5.3.3|^7.0",
"justinrainbow/json-schema": "^2.0",
"seld/jsonlint": "^1.0",
"webmozart/assert": "^1.0",
"webmozart/path-util": "^2.3"
"php": ">=7.3",
"justinrainbow/json-schema": "^5.2",
"seld/jsonlint": "^1.8",
"webmozart/assert": "^1.9",
"webmozart/path-util": "^2.3",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^4.6",
"sebastian/version": "^1.0.1",
"symfony/filesystem": "^2.5"
"phpunit/phpunit": "^9.5",
"sebastian/version": "^3.0.2",
"symfony/filesystem": "^5.2"
},
"autoload": {
"psr-4": {
Expand Down
25 changes: 0 additions & 25 deletions src/InvalidSchemaException.php

This file was deleted.

25 changes: 11 additions & 14 deletions src/JsonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Webmozart\Json;

use JsonSchema\Validator;
use Seld\JsonLint\JsonParser;
use Seld\JsonLint\ParsingException;

Expand Down Expand Up @@ -44,7 +45,7 @@ class JsonDecoder
const STRING = 3;

/**
* @var JsonValidator
* @var Validator
*/
private $validator;

Expand All @@ -66,11 +67,11 @@ class JsonDecoder
/**
* Creates a new decoder.
*
* @param null|JsonValidator $validator
* @param null|Validator $validator
*/
public function __construct(JsonValidator $validator = null)
public function __construct(Validator $validator = null)
{
$this->validator = $validator ?: new JsonValidator();
$this->validator = $validator ?? new Validator();
}

/**
Expand All @@ -94,7 +95,6 @@ public function __construct(JsonValidator $validator = null)
* @throws DecodingFailedException If the JSON string could not be decoded
* @throws ValidationFailedException If the decoded string fails schema
* validation
* @throws InvalidSchemaException If the schema is invalid
*/
public function decode($json, $schema = null)
{
Expand All @@ -109,7 +109,8 @@ public function decode($json, $schema = null)
$decoded = $this->decodeJson($json);

if (null !== $schema) {
$errors = $this->validator->validate($decoded, $schema);
$this->validator->validate($decoded, $schema);
$errors = $this->validator->getErrors();

if (count($errors) > 0) {
throw ValidationFailedException::fromErrors($errors);
Expand All @@ -131,7 +132,6 @@ public function decode($json, $schema = null)
* @throws DecodingFailedException If the file could not be decoded
* @throws ValidationFailedException If the decoded file fails schema
* validation
* @throws InvalidSchemaException If the schema is invalid
*
* @see decode
*/
Expand Down Expand Up @@ -170,6 +170,10 @@ public function decodeFile($path, $schema = null)
), $errorCode);
}

if (false === $content) {
throw new IOException(sprintf('Could not read %s', $path));
}

try {
return $this->decode($content, $schema);
} catch (DecodingFailedException $e) {
Expand All @@ -186,13 +190,6 @@ public function decodeFile($path, $schema = null)
$path,
$e->getErrorsAsString()
), $e->getErrors(), $e->getCode(), $e);
} catch (InvalidSchemaException $e) {
// Add the file name to the exception
throw new InvalidSchemaException(sprintf(
'An error happened while decoding %s: %s',
$path,
$e->getMessage()
), $e->getCode(), $e);
}
}

Expand Down
43 changes: 8 additions & 35 deletions src/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Webmozart\Json;

use JsonSchema\Validator;

/**
* Encodes data as JSON.
*
Expand Down Expand Up @@ -41,7 +43,7 @@ class JsonEncoder
const JSON_NUMBER = 4;

/**
* @var JsonValidator
* @var Validator
*/
private $validator;

Expand Down Expand Up @@ -103,11 +105,11 @@ class JsonEncoder
/**
* Creates a new encoder.
*
* @param null|JsonValidator $validator
* @param null|Validator $validator
*/
public function __construct(JsonValidator $validator = null)
public function __construct(Validator $validator = null)
{
$this->validator = $validator ?: new JsonValidator();
$this->validator = $validator ?? new Validator();
}

/**
Expand All @@ -126,12 +128,12 @@ public function __construct(JsonValidator $validator = null)
*
* @throws EncodingFailedException If the data could not be encoded
* @throws ValidationFailedException If the data fails schema validation
* @throws InvalidSchemaException If the schema is invalid
*/
public function encode($data, $schema = null)
{
if (null !== $schema) {
$errors = $this->validator->validate($data, $schema);
$this->validator->validate($data, $schema);
$errors = $this->validator->getErrors();

if (count($errors) > 0) {
throw ValidationFailedException::fromErrors($errors);
Expand Down Expand Up @@ -178,21 +180,6 @@ public function encode($data, $schema = null)
}
}

if (PHP_VERSION_ID < 71000) {
// PHP before 7.1 decodes empty properties as "_empty_". Make
// sure the encoding of these properties works again.
if (is_object($data) && isset($data->{'_empty_'})) {
$data = (array) $data;
}

if (is_array($data) && isset($data['_empty_'])) {
// Maintain key order
$keys = array_keys($data);
$keys[array_search('_empty_', $keys, true)] = '';
$data = array_combine($keys, $data);
}
}

if (PHP_VERSION_ID >= 50500) {
$maxDepth = $this->maxDepth;

Expand All @@ -210,12 +197,6 @@ public function encode($data, $schema = null)
$encoded = json_encode($data, $options);
}

if (PHP_VERSION_ID < 50400 && !$this->slashEscaped) {
// PHP below 5.4 does not allow to turn off slash escaping. Let's
// unescape slashes manually.
$encoded = str_replace('\\/', '/', $encoded);
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new EncodingFailedException(sprintf(
'The data could not be encoded as JSON: %s',
Expand All @@ -239,7 +220,6 @@ public function encode($data, $schema = null)
*
* @throws EncodingFailedException If the data could not be encoded
* @throws ValidationFailedException If the data fails schema validation
* @throws InvalidSchemaException If the schema is invalid
*
* @see encode
*/
Expand Down Expand Up @@ -268,13 +248,6 @@ public function encodeFile($data, $path, $schema = null)
$path,
$e->getErrorsAsString()
), $e->getErrors(), $e->getCode(), $e);
} catch (InvalidSchemaException $e) {
// Add the file name to the exception
throw new InvalidSchemaException(sprintf(
'An error happened while encoding %s: %s',
$path,
$e->getMessage()
), $e->getCode(), $e);
}

$errorMessage = null;
Expand Down
Loading