diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..cc456a56 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +phpcs: vendor + vendor/bin/phpcs -s --standard=PSR12 --extensions=php bin demo src tests + +vendor: composer.json + composer update + touch $@ diff --git a/composer.json b/composer.json index a9184092..59946f84 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/JsonSchema/ConstraintError.php b/src/JsonSchema/ConstraintError.php index fd5ba8e6..18521ec8 100644 --- a/src/JsonSchema/ConstraintError.php +++ b/src/JsonSchema/ConstraintError.php @@ -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', @@ -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', diff --git a/src/JsonSchema/Constraints/CollectionConstraint.php b/src/JsonSchema/Constraints/CollectionConstraint.php index 825d4531..a4317de2 100644 --- a/src/JsonSchema/Constraints/CollectionConstraint.php +++ b/src/JsonSchema/Constraints/CollectionConstraint.php @@ -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); } diff --git a/src/JsonSchema/Constraints/Constraint.php b/src/JsonSchema/Constraints/Constraint.php index 389e0f53..360f263c 100644 --- a/src/JsonSchema/Constraints/Constraint.php +++ b/src/JsonSchema/Constraints/Constraint.php @@ -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 @@ -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()); } @@ -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()); } diff --git a/src/JsonSchema/Constraints/FormatConstraint.php b/src/JsonSchema/Constraints/FormatConstraint.php index a55356d1..19027822 100644 --- a/src/JsonSchema/Constraints/FormatConstraint.php +++ b/src/JsonSchema/Constraints/FormatConstraint.php @@ -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 ) @@ -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, ) @@ -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 ) @@ -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 ) @@ -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; @@ -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; } @@ -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); diff --git a/src/JsonSchema/Constraints/ObjectConstraint.php b/src/JsonSchema/Constraints/ObjectConstraint.php index dd1c02b9..ef6e2fcf 100644 --- a/src/JsonSchema/Constraints/ObjectConstraint.php +++ b/src/JsonSchema/Constraints/ObjectConstraint.php @@ -28,9 +28,15 @@ class ObjectConstraint extends Constraint /** * {@inheritdoc} */ - public function check(&$element, $schema = null, JsonPointer $path = null, $properties = null, - $additionalProp = null, $patternProperties = null, $appliedDefaults = array()) - { + public function check( + &$element, + $schema = null, + JsonPointer $path = null, + $properties = null, + $additionalProp = null, + $patternProperties = null, + $appliedDefaults = array() + ) { if ($element instanceof UndefinedConstraint) { return; } @@ -73,7 +79,13 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p foreach ($element as $i => $value) { if (preg_match($delimiter . $pregex . $delimiter . 'u', $i)) { $matches[] = $i; - $this->checkUndefined($value, $schema ?: new \stdClass(), $path, $i, in_array($i, $this->appliedDefaults)); + $this->checkUndefined( + $value, + $schema ?: new \stdClass(), + $path, + $i, + in_array($i, $this->appliedDefaults) + ); } } } @@ -91,16 +103,26 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p * @param \StdClass $properties Properties * @param mixed $additionalProp Additional properties */ - public function validateElement($element, $matches, $schema = null, JsonPointer $path = null, - $properties = null, $additionalProp = null) - { + public function validateElement( + $element, + $matches, + $schema = null, + JsonPointer $path = null, + $properties = null, + $additionalProp = null + ) { $this->validateMinMaxConstraint($element, $schema, $path); foreach ($element as $i => $value) { $definition = $this->getProperty($properties, $i); // no additional properties allowed - if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) { + if ( + !in_array($i, $matches) + && $additionalProp === false + && $this->inlineSchemaProperty !== $i + && !$definition + ) { $this->addError(ConstraintError::ADDITIONAL_PROPERTIES(), $path, array('property' => $i)); } @@ -124,7 +146,11 @@ public function validateElement($element, $matches, $schema = null, JsonPointer $property = $this->getProperty($element, $i, $this->factory->createInstanceFor('undefined')); if (is_object($property)) { - $this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path); + $this->validateMinMaxConstraint( + !($property instanceof UndefinedConstraint) ? $property : $element, + $definition, + $path + ); } } } @@ -162,7 +188,12 @@ public function validateProperties(&$element, $properties = null, JsonPointer $p */ protected function &getProperty(&$element, $property, $fallback = null) { - if (is_array($element) && (isset($element[$property]) || array_key_exists($property, $element)) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) { + if ( + is_array($element) + && (isset($element[$property]) + || array_key_exists($property, $element)) + /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/ + ) { return $element[$property]; } elseif (is_object($element) && property_exists($element, $property)) { return $element->$property; @@ -183,13 +214,21 @@ protected function validateMinMaxConstraint($element, $objectDefinition, JsonPoi // Verify minimum number of properties if (isset($objectDefinition->minProperties) && !is_object($objectDefinition->minProperties)) { if ($this->getTypeCheck()->propertyCount($element) < $objectDefinition->minProperties) { - $this->addError(ConstraintError::PROPERTIES_MIN(), $path, array('minProperties' => $objectDefinition->minProperties)); + $this->addError( + ConstraintError::PROPERTIES_MIN(), + $path, + array('minProperties' => $objectDefinition->minProperties) + ); } } // Verify maximum number of properties if (isset($objectDefinition->maxProperties) && !is_object($objectDefinition->maxProperties)) { if ($this->getTypeCheck()->propertyCount($element) > $objectDefinition->maxProperties) { - $this->addError(ConstraintError::PROPERTIES_MAX(), $path, array('maxProperties' => $objectDefinition->maxProperties)); + $this->addError( + ConstraintError::PROPERTIES_MAX(), + $path, + array('maxProperties' => $objectDefinition->maxProperties) + ); } } } diff --git a/src/JsonSchema/Constraints/SchemaConstraint.php b/src/JsonSchema/Constraints/SchemaConstraint.php index 425c38f2..804a576e 100644 --- a/src/JsonSchema/Constraints/SchemaConstraint.php +++ b/src/JsonSchema/Constraints/SchemaConstraint.php @@ -24,7 +24,7 @@ */ class SchemaConstraint extends Constraint { - const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema#'; + private const DEFAULT_SCHEMA_SPEC = 'http://json-schema.org/draft-04/schema#'; /** * {@inheritdoc} diff --git a/src/JsonSchema/Constraints/TypeConstraint.php b/src/JsonSchema/Constraints/TypeConstraint.php index ff4eba71..342c0d83 100644 --- a/src/JsonSchema/Constraints/TypeConstraint.php +++ b/src/JsonSchema/Constraints/TypeConstraint.php @@ -155,7 +155,8 @@ protected function validateTypeNameWording($type) sprintf( 'No wording for %s available, expected wordings are: [%s]', var_export($type, true), - implode(', ', array_filter(self::$wording))) + implode(', ', array_filter(self::$wording)) + ) ); } } @@ -233,7 +234,9 @@ protected function validateType(&$value, $type, $coerce = false) return is_null($value); } - throw new InvalidArgumentException((is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type); + throw new InvalidArgumentException( + (is_object($value) ? 'object' : $value) . ' is an invalid type for ' . $type + ); } /** diff --git a/src/JsonSchema/Constraints/UndefinedConstraint.php b/src/JsonSchema/Constraints/UndefinedConstraint.php index bdb95f4a..19461778 100644 --- a/src/JsonSchema/Constraints/UndefinedConstraint.php +++ b/src/JsonSchema/Constraints/UndefinedConstraint.php @@ -140,7 +140,8 @@ protected function validateCommonProperties(&$value, $schema, JsonPointer $path, if (!$this->getTypeCheck()->propertyExists($value, $required)) { $this->addError( ConstraintError::REQUIRED(), - $this->incrementPath($path ?: new JsonPointer(''), $required), array( + $this->incrementPath($path ?: new JsonPointer(''), $required), + array( 'property' => $required ) ); @@ -280,7 +281,8 @@ protected function applyDefaultValues(&$value, $schema, $path) if ( !array_key_exists($currentItem, $value) && property_exists($itemDefinition, 'default') - && $this->shouldApplyDefaultValue($requiredOnly, $itemDefinition)) { + && $this->shouldApplyDefaultValue($requiredOnly, $itemDefinition) + ) { if (is_object($itemDefinition->default)) { $value[$currentItem] = clone $itemDefinition->default; } else { @@ -292,7 +294,8 @@ protected function applyDefaultValues(&$value, $schema, $path) } elseif ( $value instanceof self && property_exists($schema, 'default') - && $this->shouldApplyDefaultValue($requiredOnly, $schema)) { + && $this->shouldApplyDefaultValue($requiredOnly, $schema) + ) { // $value is a leaf, not a container - apply the default directly $value = is_object($schema->default) ? clone $schema->default : $schema->default; $path->setFromDefault(); diff --git a/src/JsonSchema/Rfc3339.php b/src/JsonSchema/Rfc3339.php index adca581a..8c2c4708 100644 --- a/src/JsonSchema/Rfc3339.php +++ b/src/JsonSchema/Rfc3339.php @@ -4,7 +4,7 @@ class Rfc3339 { - const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/'; + private const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/'; /** * Try creating a DateTime instance @@ -23,7 +23,11 @@ public static function createFromString($string) $microseconds = $matches[2] ?: '.000000'; $timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00'; $dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP'; - $dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC')); + $dateTime = \DateTime::createFromFormat( + $dateFormat, + $dateAndTime . $microseconds . $timeZone, + new \DateTimeZone('UTC') + ); return $dateTime ?: null; } diff --git a/src/JsonSchema/SchemaStorage.php b/src/JsonSchema/SchemaStorage.php index 81b3508c..660b05a4 100644 --- a/src/JsonSchema/SchemaStorage.php +++ b/src/JsonSchema/SchemaStorage.php @@ -10,7 +10,7 @@ class SchemaStorage implements SchemaStorageInterface { - const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema/'; + public const INTERNAL_PROVIDED_SCHEMA_URI = 'internal://provided-schema/'; protected $uriRetriever; protected $uriResolver; diff --git a/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php b/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php index 9ab24b95..84220305 100644 --- a/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php +++ b/src/JsonSchema/Uri/Retrievers/AbstractRetriever.php @@ -1,4 +1,5 @@ = count($basePathSegments)) { - throw new UriResolverException(sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath)); + throw new UriResolverException( + sprintf("Unable to resolve URI '%s' from base '%s'", $relativePath, $basePath) + ); } $basePathSegments = array_slice($basePathSegments, 0, -$numLevelUp); diff --git a/src/JsonSchema/Validator.php b/src/JsonSchema/Validator.php index 7b31279c..17dff577 100644 --- a/src/JsonSchema/Validator.php +++ b/src/JsonSchema/Validator.php @@ -22,12 +22,12 @@ */ class Validator extends BaseConstraint { - const SCHEMA_MEDIA_TYPE = 'application/schema+json'; + public const SCHEMA_MEDIA_TYPE = 'application/schema+json'; - const ERROR_NONE = 0x00000000; - const ERROR_ALL = 0xFFFFFFFF; - const ERROR_DOCUMENT_VALIDATION = 0x00000001; - const ERROR_SCHEMA_VALIDATION = 0x00000002; + public const ERROR_NONE = 0x00000000; + public const ERROR_ALL = 0xFFFFFFFF; + public const ERROR_DOCUMENT_VALIDATION = 0x00000001; + public const ERROR_SCHEMA_VALIDATION = 0x00000002; /** * Validates the given data against the schema and returns an object containing the results diff --git a/tests/Constraints/AdditionalPropertiesTest.php b/tests/Constraints/AdditionalPropertiesTest.php index e064312d..be8fb64f 100644 --- a/tests/Constraints/AdditionalPropertiesTest.php +++ b/tests/Constraints/AdditionalPropertiesTest.php @@ -39,6 +39,7 @@ public function getInvalidTests() array( 'property' => '', 'pointer' => '', + // phpcs:ignore Generic.Files.LineLength.TooLong 'message' => 'The property additionalProp is not defined and the definition does not allow additional properties', 'constraint' => array( 'name' => 'additionalProp', diff --git a/tests/Constraints/BaseTestCase.php b/tests/Constraints/BaseTestCase.php index 50efbd82..04d3a739 100644 --- a/tests/Constraints/BaseTestCase.php +++ b/tests/Constraints/BaseTestCase.php @@ -55,6 +55,7 @@ public function testInvalidCases($input, $schema, $checkMode = Constraint::CHECK /** * @dataProvider getInvalidForAssocTests */ + // phpcs:ignore Generic.Files.LineLength.TooLong public function testInvalidCasesUsingAssoc($input, $schema, $checkMode = Constraint::CHECK_MODE_TYPE_CAST, $errors = array()) { $checkMode = $checkMode === null ? Constraint::CHECK_MODE_TYPE_CAST : $checkMode; diff --git a/tests/Constraints/CoerciveTest.php b/tests/Constraints/CoerciveTest.php index 22684b3e..e484df5c 100644 --- a/tests/Constraints/CoerciveTest.php +++ b/tests/Constraints/CoerciveTest.php @@ -39,7 +39,7 @@ public function dataCoerceCases() array('array', '[45]', '45', true), // #5 array('object', '{"a":"b"}', null, false), // #6 array('array', '[{"a":"b"}]', null, false), // #7 - array('array', '[1,2]', array(1, 2), false), // #8 + array('array', '[1,2]', array(1, 2), false), // #8 ), 'integer' => array( array('string', '"45"', 45, true), // #9 @@ -190,6 +190,7 @@ public function dataCoerceCases() } /** @dataProvider dataCoerceCases **/ + // phpcs:ignore Generic.Files.LineLength.TooLong public function testCoerceCases($schema, $data, $startType, $endType, $endValue, $valid, $extraFlags = 0, $assoc = false) { $validator = new Validator($this->factory); diff --git a/tests/Constraints/FactoryTest.php b/tests/Constraints/FactoryTest.php index adc11844..f002578a 100644 --- a/tests/Constraints/FactoryTest.php +++ b/tests/Constraints/FactoryTest.php @@ -28,6 +28,7 @@ class MyBadConstraint * * @package JsonSchema\Tests\Constraints */ +// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses class MyStringConstraint extends Constraint { public function check(&$value, $schema = null, JsonPointer $path = null, $i = null) @@ -35,6 +36,7 @@ public function check(&$value, $schema = null, JsonPointer $path = null, $i = nu } } +// phpcs:ignore PSR1.Classes.ClassDeclaration.MultipleClasses class FactoryTest extends TestCase { /** diff --git a/tests/Constraints/OfPropertiesTest.php b/tests/Constraints/OfPropertiesTest.php index ff8bded3..7c15da8a 100644 --- a/tests/Constraints/OfPropertiesTest.php +++ b/tests/Constraints/OfPropertiesTest.php @@ -1,4 +1,5 @@ getErrors(); + // phpcs:ignore Generic.Files.LineLength.TooLong $this->assertCount(1, $actualErrors, 'Failed to assert that Type has exactly one error to assert the error message against.'); $actualError = $actualErrors[0]; + // phpcs:ignore Generic.Files.LineLength.TooLong $this->assertInternalType('array', $actualError, sprintf('Failed to assert that Type error is an array, %s given', gettype($actualError))); $messageKey = 'message'; $this->assertArrayHasKey( - $messageKey, $actualError, + $messageKey, + $actualError, sprintf('Failed to assert that Type error has a message key %s.', var_export($messageKey, true)) ); @@ -103,6 +106,7 @@ public function testValidateTypeNameWording() $this->setExpectedException( '\UnexpectedValueException', + // phpcs:ignore Generic.Files.LineLength.TooLong "No wording for 'notAValidTypeName' available, expected wordings are: [an integer, a number, a boolean, an object, an array, a string, a null]" ); $m->invoke($t, 'notAValidTypeName'); diff --git a/tests/Iterators/ObjectIteratorTest.php b/tests/Iterators/ObjectIteratorTest.php index e36d2ea4..359796c6 100644 --- a/tests/Iterators/ObjectIteratorTest.php +++ b/tests/Iterators/ObjectIteratorTest.php @@ -69,7 +69,7 @@ public function testKey() public function testAlwaysObjects() { - $i= new ObjectIterator($this->testObject); + $i = new ObjectIterator($this->testObject); foreach ($i as $item) { $this->assertInstanceOf('\StdClass', $item); diff --git a/tests/Rfc3339Test.php b/tests/Rfc3339Test.php index e489d389..53a8988c 100644 --- a/tests/Rfc3339Test.php +++ b/tests/Rfc3339Test.php @@ -26,7 +26,10 @@ public function testCreateFromValidString($string, \DateTime $expected) */ public function testCreateFromInvalidString($string) { - $this->assertNull(Rfc3339::createFromString($string), sprintf('String "%s" should not be converted to DateTime', $string)); + $this->assertNull( + Rfc3339::createFromString($string), + sprintf('String "%s" should not be converted to DateTime', $string) + ); } public function provideValidFormats() diff --git a/tests/SchemaStorageTest.php b/tests/SchemaStorageTest.php index ebbc9477..ee4b271f 100644 --- a/tests/SchemaStorageTest.php +++ b/tests/SchemaStorageTest.php @@ -181,10 +181,10 @@ private function getMainSchema() ) ), 'yardproperties' => (object) array( - 'tree'=>(object) array( + 'tree' => (object) array( 'type' => 'string' ), - 'pool'=>(object) array( + 'pool' => (object) array( 'type' => 'string' ) ) diff --git a/tests/Uri/Retrievers/FileGetContentsTest.php b/tests/Uri/Retrievers/FileGetContentsTest.php index 0514a7d5..f727541f 100644 --- a/tests/Uri/Retrievers/FileGetContentsTest.php +++ b/tests/Uri/Retrievers/FileGetContentsTest.php @@ -67,9 +67,12 @@ public function testContentType() function file_get_contents($uri) { switch ($uri) { - case 'http://example.com/false': return false; - case 'file:///this/is/a/directory/': return ''; - default: return \file_get_contents($uri); + case 'http://example.com/false': + return false; + case 'file:///this/is/a/directory/': + return ''; + default: + return \file_get_contents($uri); } } } diff --git a/tests/Uri/UriRetrieverTest.php b/tests/Uri/UriRetrieverTest.php index 24714a26..4ca8b7bb 100644 --- a/tests/Uri/UriRetrieverTest.php +++ b/tests/Uri/UriRetrieverTest.php @@ -157,7 +157,8 @@ public function testResolvePointerNoFragment() $this->assertEquals( $schema, $retriever->resolvePointer( - $schema, 'http://example.org/schema.json' + $schema, + 'http://example.org/schema.json' ) ); } @@ -177,7 +178,8 @@ public function testResolvePointerFragment() $this->assertEquals( $schema->definitions->foo, $retriever->resolvePointer( - $schema, 'http://example.org/schema.json#/definitions/foo' + $schema, + 'http://example.org/schema.json#/definitions/foo' ) ); } @@ -198,7 +200,8 @@ public function testResolvePointerFragmentNotFound() $retriever = new \JsonSchema\Uri\UriRetriever(); $retriever->resolvePointer( - $schema, 'http://example.org/schema.json#/definitions/bar' + $schema, + 'http://example.org/schema.json#/definitions/bar' ); } @@ -218,7 +221,8 @@ public function testResolvePointerFragmentNoArray() $retriever = new \JsonSchema\Uri\UriRetriever(); $retriever->resolvePointer( - $schema, 'http://example.org/schema.json#/definitions/foo' + $schema, + 'http://example.org/schema.json#/definitions/foo' ); } @@ -229,7 +233,8 @@ public function testResolveExcessLevelUp() { $retriever = new \JsonSchema\Uri\UriRetriever(); $retriever->resolve( - '../schema.json#', 'http://example.org/schema.json#' + '../schema.json#', + 'http://example.org/schema.json#' ); }