Skip to content
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

Add codesniffer; fix phpcs errors wrt PSR12 #660

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
phpcs: vendor
vendor/bin/phpcs -s --standard=PSR12 --extensions=php bin demo src tests

vendor: composer.json
composer update
touch $@
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1",
"json-schema/json-schema-test-suite": "1.2.0",
"phpunit/phpunit": "^4.8.35"
"phpunit/phpunit": "^4.8.35",
"squizlabs/php_codesniffer": "~3.5"
},
"extra": {
"branch-alias": {
Expand Down
95 changes: 49 additions & 46 deletions src/JsonSchema/ConstraintError.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,59 @@

class ConstraintError extends Enum
{
const ADDITIONAL_ITEMS = 'additionalItems';
const ADDITIONAL_PROPERTIES = 'additionalProp';
const ALL_OF = 'allOf';
const ANY_OF = 'anyOf';
const DEPENDENCIES = 'dependencies';
const DISALLOW = 'disallow';
const DIVISIBLE_BY = 'divisibleBy';
const ENUM = 'enum';
const CONSTANT = 'const';
const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
const FORMAT_COLOR = 'colorFormat';
const FORMAT_DATE = 'dateFormat';
const FORMAT_DATE_TIME = 'dateTimeFormat';
const FORMAT_DATE_UTC = 'dateUtcFormat';
const FORMAT_EMAIL = 'emailFormat';
const FORMAT_HOSTNAME = 'styleHostName';
const FORMAT_IP = 'ipFormat';
const FORMAT_PHONE = 'phoneFormat';
const FORMAT_REGEX= 'regexFormat';
const FORMAT_STYLE = 'styleFormat';
const FORMAT_TIME = 'timeFormat';
const FORMAT_URL = 'urlFormat';
const FORMAT_URL_REF = 'urlRefFormat';
const INVALID_SCHEMA = 'invalidSchema';
const LENGTH_MAX = 'maxLength';
const LENGTH_MIN = 'minLength';
const MAXIMUM = 'maximum';
const MIN_ITEMS = 'minItems';
const MINIMUM = 'minimum';
const MISSING_ERROR = 'missingError';
const MISSING_MAXIMUM = 'missingMaximum';
const MISSING_MINIMUM = 'missingMinimum';
const MAX_ITEMS = 'maxItems';
const MULTIPLE_OF = 'multipleOf';
const NOT = 'not';
const ONE_OF = 'oneOf';
const REQUIRED = 'required';
const REQUIRES = 'requires';
const PATTERN = 'pattern';
const PREGEX_INVALID = 'pregrex';
const PROPERTIES_MIN = 'minProperties';
const PROPERTIES_MAX = 'maxProperties';
const TYPE = 'type';
const UNIQUE_ITEMS = 'uniqueItems';
public const ADDITIONAL_ITEMS = 'additionalItems';
public const ADDITIONAL_PROPERTIES = 'additionalProp';
public const ALL_OF = 'allOf';
public const ANY_OF = 'anyOf';
public const DEPENDENCIES = 'dependencies';
public const DISALLOW = 'disallow';
public const DIVISIBLE_BY = 'divisibleBy';
public const ENUM = 'enum';
public const CONSTANT = 'const';
public const EXCLUSIVE_MINIMUM = 'exclusiveMinimum';
public const EXCLUSIVE_MAXIMUM = 'exclusiveMaximum';
public const FORMAT_COLOR = 'colorFormat';
public const FORMAT_DATE = 'dateFormat';
public const FORMAT_DATE_TIME = 'dateTimeFormat';
public const FORMAT_DATE_UTC = 'dateUtcFormat';
public const FORMAT_EMAIL = 'emailFormat';
public const FORMAT_HOSTNAME = 'styleHostName';
public const FORMAT_IP = 'ipFormat';
public const FORMAT_PHONE = 'phoneFormat';
public const FORMAT_REGEX = 'regexFormat';
public const FORMAT_STYLE = 'styleFormat';
public const FORMAT_TIME = 'timeFormat';
public const FORMAT_URL = 'urlFormat';
public const FORMAT_URL_REF = 'urlRefFormat';
public const INVALID_SCHEMA = 'invalidSchema';
public const LENGTH_MAX = 'maxLength';
public const LENGTH_MIN = 'minLength';
public const MAXIMUM = 'maximum';
public const MIN_ITEMS = 'minItems';
public const MINIMUM = 'minimum';
public const MISSING_ERROR = 'missingError';
public const MISSING_MAXIMUM = 'missingMaximum';
public const MISSING_MINIMUM = 'missingMinimum';
public const MAX_ITEMS = 'maxItems';
public const MULTIPLE_OF = 'multipleOf';
public const NOT = 'not';
public const ONE_OF = 'oneOf';
public const REQUIRED = 'required';
public const REQUIRES = 'requires';
public const PATTERN = 'pattern';
public const PREGEX_INVALID = 'pregrex';
public const PROPERTIES_MIN = 'minProperties';
public const PROPERTIES_MAX = 'maxProperties';
public const TYPE = 'type';
public const UNIQUE_ITEMS = 'uniqueItems';

public function getMessage()
{
$name = $this->getValue();
static $messages = array(
// phpcs:ignore Generic.Files.LineLength.TooLong
self::ADDITIONAL_ITEMS => 'The item %s[%s] is not defined and the definition does not allow additional items',
// phpcs:ignore Generic.Files.LineLength.TooLong
self::ADDITIONAL_PROPERTIES => 'The property %s is not defined and the definition does not allow additional properties',
self::ALL_OF => 'Failed to match all schemas',
self::ANY_OF => 'Failed to match at least one schema',
Expand All @@ -69,13 +71,14 @@ public function getMessage()
self::EXCLUSIVE_MAXIMUM => 'Must have a maximum value less than %d',
self::FORMAT_COLOR => 'Invalid color',
self::FORMAT_DATE => 'Invalid date %s, expected format YYYY-MM-DD',
// phpcs:ignore Generic.Files.LineLength.TooLong
self::FORMAT_DATE_TIME => 'Invalid date-time %s, expected format YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss+hh:mm',
self::FORMAT_DATE_UTC => 'Invalid time %s, expected integer of milliseconds since Epoch',
self::FORMAT_EMAIL => 'Invalid email',
self::FORMAT_HOSTNAME => 'Invalid hostname',
self::FORMAT_IP => 'Invalid IP address',
self::FORMAT_PHONE => 'Invalid phone number',
self::FORMAT_REGEX=> 'Invalid regex format %s',
self::FORMAT_REGEX => 'Invalid regex format %s',
self::FORMAT_STYLE => 'Invalid style',
self::FORMAT_TIME => 'Invalid time %s, expected format hh:mm:ss',
self::FORMAT_URL => 'Invalid URL format',
Expand Down
6 changes: 5 additions & 1 deletion src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu
$this->checkUndefined($v, $schema->items, $path, $k);

// Recheck with "additionalItems" if the first test fails
if (count($initErrors) < count($this->getErrors()) && (isset($schema->additionalItems) && $schema->additionalItems !== false)) {
if (
count($initErrors) < count($this->getErrors())
&& (isset($schema->additionalItems)
&& $schema->additionalItems !== false)
) {
$secondErrors = $this->getErrors();
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
}
Expand Down
59 changes: 42 additions & 17 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ abstract class Constraint extends BaseConstraint implements ConstraintInterface
{
protected $inlineSchemaProperty = '$schema';

const CHECK_MODE_NONE = 0x00000000;
const CHECK_MODE_NORMAL = 0x00000001;
const CHECK_MODE_TYPE_CAST = 0x00000002;
const CHECK_MODE_COERCE_TYPES = 0x00000004;
const CHECK_MODE_APPLY_DEFAULTS = 0x00000008;
const CHECK_MODE_EXCEPTIONS = 0x00000010;
const CHECK_MODE_DISABLE_FORMAT = 0x00000020;
const CHECK_MODE_EARLY_COERCE = 0x00000040;
const CHECK_MODE_ONLY_REQUIRED_DEFAULTS = 0x00000080;
const CHECK_MODE_VALIDATE_SCHEMA = 0x00000100;
public const CHECK_MODE_NONE = 0x00000000;
public const CHECK_MODE_NORMAL = 0x00000001;
public const CHECK_MODE_TYPE_CAST = 0x00000002;
public const CHECK_MODE_COERCE_TYPES = 0x00000004;
public const CHECK_MODE_APPLY_DEFAULTS = 0x00000008;
public const CHECK_MODE_EXCEPTIONS = 0x00000010;
public const CHECK_MODE_DISABLE_FORMAT = 0x00000020;
public const CHECK_MODE_EARLY_COERCE = 0x00000040;
public const CHECK_MODE_ONLY_REQUIRED_DEFAULTS = 0x00000080;
public const CHECK_MODE_VALIDATE_SCHEMA = 0x00000100;

/**
* Bubble down the path
Expand Down Expand Up @@ -79,11 +79,25 @@ protected function checkArray(&$value, $schema = null, JsonPointer $path = null,
* @param mixed $additionalProperties
* @param mixed $patternProperties
*/
protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
$additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
{
protected function checkObject(
&$value,
$schema = null,
JsonPointer $path = null,
$properties = null,
$additionalProperties = null,
$patternProperties = null,
$appliedDefaults = array()
) {
$validator = $this->factory->createInstanceFor('object');
$validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);
$validator->check(
$value,
$schema,
$path,
$properties,
$additionalProperties,
$patternProperties,
$appliedDefaults
);

$this->addErrors($validator->getErrors());
}
Expand Down Expand Up @@ -112,11 +126,22 @@ protected function checkType(&$value, $schema = null, JsonPointer $path = null,
* @param JsonPointer|null $path
* @param mixed $i
*/
protected function checkUndefined(&$value, $schema = null, JsonPointer $path = null, $i = null, $fromDefault = false)
{
protected function checkUndefined(
&$value,
$schema = null,
JsonPointer $path = null,
$i = null,
$fromDefault = false
) {
$validator = $this->factory->createInstanceFor('undefined');

$validator->check($value, $this->factory->getSchemaStorage()->resolveRefSchema($schema), $path, $i, $fromDefault);
$validator->check(
$value,
$this->factory->getSchemaStorage()->resolveRefSchema($schema),
$path,
$i,
$fromDefault
);

$this->addErrors($validator->getErrors());
}
Expand Down
33 changes: 26 additions & 7 deletions src/JsonSchema/Constraints/FormatConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
switch ($schema->format) {
case 'date':
if (!$date = $this->validateDateTime($element, 'Y-m-d')) {
$this->addError(ConstraintError::FORMAT_DATE(), $path, array(
$this->addError(
ConstraintError::FORMAT_DATE(),
$path,
array(
'date' => $element,
'format' => $schema->format
)
Expand All @@ -44,7 +47,10 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =

case 'time':
if (!$this->validateDateTime($element, 'H:i:s')) {
$this->addError(ConstraintError::FORMAT_TIME(), $path, array(
$this->addError(
ConstraintError::FORMAT_TIME(),
$path,
array(
'time' => json_encode($element),
'format' => $schema->format,
)
Expand All @@ -54,7 +60,10 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =

case 'date-time':
if (null === Rfc3339::createFromString($element)) {
$this->addError(ConstraintError::FORMAT_DATE_TIME(), $path, array(
$this->addError(
ConstraintError::FORMAT_DATE_TIME(),
$path,
array(
'dateTime' => json_encode($element),
'format' => $schema->format
)
Expand All @@ -72,7 +81,10 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =

case 'regex':
if (!$this->validateRegex($element)) {
$this->addError(ConstraintError::FORMAT_REGEX(), $path, array(
$this->addError(
ConstraintError::FORMAT_REGEX(),
$path,
array(
'value' => $element,
'format' => $schema->format
)
Expand Down Expand Up @@ -119,7 +131,11 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
if (strpos($pathParts[0], ':') !== false) {
$validURL = null;
} else {
$validURL = filter_var('scheme://host/' . $element, FILTER_VALIDATE_URL, FILTER_NULL_ON_FAILURE);
$validURL = filter_var(
'scheme://host/' . $element,
FILTER_VALIDATE_URL,
FILTER_NULL_ON_FAILURE
);
}
} else {
$validURL = null;
Expand Down Expand Up @@ -202,9 +218,11 @@ protected function validateRegex($regex)

protected function validateColor($color)
{
if (in_array(strtolower($color), array('aqua', 'black', 'blue', 'fuchsia',
if (
in_array(strtolower($color), array('aqua', 'black', 'blue', 'fuchsia',
'gray', 'green', 'lime', 'maroon', 'navy', 'olive', 'orange', 'purple',
'red', 'silver', 'teal', 'white', 'yellow'))) {
'red', 'silver', 'teal', 'white', 'yellow'))
) {
return true;
}

Expand All @@ -226,6 +244,7 @@ protected function validatePhone($phone)

protected function validateHostname($host)
{
// phpcs:ignore Generic.Files.LineLength.TooLong
$hostnameRegex = '/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/i';

return preg_match($hostnameRegex, $host);
Expand Down
Loading