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

Make code-style more consistent #355

Merged
merged 4 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ coverage
.buildpath
.project
.settings
.php_cs
.php_cs.cache
composer.lock
docs-api
phpunit.xml
31 changes: 31 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

$finder = new PhpCsFixer\Finder();
$config = new PhpCsFixer\Config('json-schema', 'json-schema style guide');
$finder->in(__DIR__);

/* Based on ^2.1 of php-cs-fixer */
$config
->setRules(array(
// default
'@PSR2' => true,
'@Symfony' => true,
// additionally
'array_syntax' => array('syntax' => 'long'),
'binary_operator_spaces' => false,
'concat_space' => array('spacing' => 'one'),
'no_unused_imports' => false,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'phpdoc_no_package' => false,
'phpdoc_order' => true,
'phpdoc_summary' => false,
'pre_increment' => false,
'trailing_comma_in_multiline_array' => false,
'simplified_null_return' => false,
))
->setFinder($finder)
;

return $config;
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ language: php
cache:
directories:
- $HOME/.composer/cache
- $HOME/.phpcsfixer

matrix:
fast_finish: true
Expand All @@ -13,7 +14,7 @@ matrix:
- php: 5.5
- php: 5.6
- php: 7.0
env: WITH_COVERAGE=true
env: WITH_COVERAGE=true WITH_PHPCSFIXER=true
- php: 7.1
- php: 'nightly'
- php: hhvm
Expand All @@ -29,3 +30,4 @@ install:

script:
- if [[ "$WITH_COVERAGE" == "true" ]]; then ./vendor/bin/phpunit --coverage-text; else composer test; fi
- if [[ "$WITH_PHPCSFIXER" == "true" ]]; then composer require friendsofphp/php-cs-fixer:^2.1 && mkdir -p $HOME/.phpcsfixer && vendor/bin/php-cs-fixer fix --cache-file "$HOME/.phpcsfixer/.php_cs.cache" --dry-run --diff --verbose; fi
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
"bin": ["bin/validate-json"],
"extra": {
"branch-alias": {
"dev-master": "4.0.x-dev"
"dev-master": "5.0.x-dev"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks

}
},
"scripts": {
"test" : "vendor/bin/phpunit",
"testOnly" : "vendor/bin/phpunit --colors --filter",
"coverage" : "vendor/bin/phpunit --coverage-text"
"coverage" : "vendor/bin/phpunit --coverage-text"
}
}
7 changes: 4 additions & 3 deletions demo/demo.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
require(__DIR__ . '/../vendor/autoload.php');

require __DIR__ . '/../vendor/autoload.php';

$data = json_decode(file_get_contents('data.json'));

// Validate
$validator = new JsonSchema\Validator;
$validator->check($data, (object)['$ref' => 'file://' . realpath('schema.json')]);
$validator = new JsonSchema\Validator();
$validator->check($data, (object) array('$ref' => 'file://' . realpath('schema.json')));

if ($validator->isValid()) {
echo "The supplied JSON validates against the schema.\n";
Expand Down
19 changes: 3 additions & 16 deletions src/JsonSchema/Constraints/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ class BaseConstraint
*/
public function __construct(Factory $factory = null)
{
$this->factory = $factory ? : new Factory();
$this->factory = $factory ?: new Factory();
}

/**
* {@inheritDoc}
*/
public function addError(JsonPointer $path = null, $message, $constraint = '', array $more = null)
{
$error = array(
Expand All @@ -49,38 +46,28 @@ public function addError(JsonPointer $path = null, $message, $constraint = '', a
);

if ($this->factory->getConfig(Constraint::CHECK_MODE_EXCEPTIONS)) {
throw new ValidationException(sprintf("Error validating %s: %s", $error['pointer'], $error['message']));
throw new ValidationException(sprintf('Error validating %s: %s', $error['pointer'], $error['message']));
}

if (is_array($more) && count($more) > 0)
{
if (is_array($more) && count($more) > 0) {
$error += $more;
}

$this->errors[] = $error;
}

/**
* {@inheritDoc}
*/
public function addErrors(array $errors)
{
if ($errors) {
$this->errors = array_merge($this->errors, $errors);
}
}

/**
* {@inheritDoc}
*/
public function getErrors()
{
return $this->errors;
}

/**
* {@inheritDoc}
*/
public function isValid()
{
return !$this->getErrors();
Expand Down
15 changes: 8 additions & 7 deletions src/JsonSchema/Constraints/CollectionConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@
*/
class CollectionConstraint extends Constraint
{

/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function check(&$value, $schema = null, JsonPointer $path = null, $i = null)
{
// Verify minItems
if (isset($schema->minItems) && count($value) < $schema->minItems) {
$this->addError($path, "There must be a minimum of " . $schema->minItems . " items in the array", 'minItems', array('minItems' => $schema->minItems,));
$this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array('minItems' => $schema->minItems));
}

// Verify maxItems
if (isset($schema->maxItems) && count($value) > $schema->maxItems) {
$this->addError($path, "There must be a maximum of " . $schema->maxItems . " items in the array", 'maxItems', array('maxItems' => $schema->maxItems,));
$this->addError($path, 'There must be a maximum of ' . $schema->maxItems . ' items in the array', 'maxItems', array('maxItems' => $schema->maxItems));
}

// Verify uniqueItems
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
$unique = $value;
if (is_array($value) && count($value)) {
$unique = array_map(function($e) { return var_export($e, true); }, $value);
$unique = array_map(function ($e) {
return var_export($e, true);
}, $value);
}
if (count(array_unique($unique)) != count($value)) {
$this->addError($path, "There are no duplicates allowed in the array", 'uniqueItems');
$this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems');
}
}

Expand Down Expand Up @@ -123,7 +124,7 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
} else {
$this->addError(
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,));
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems));
}
} else {
// Should be valid against an empty schema
Expand Down
7 changes: 5 additions & 2 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;
use JsonSchema\SchemaStorage;
use JsonSchema\Uri\UriRetriever;
use JsonSchema\UriRetrieverInterface;
use JsonSchema\Entity\JsonPointer;

/**
* The Base Constraints, all Validators should extend this class
Expand Down Expand Up @@ -48,6 +48,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
array_filter(array($i), 'strlen')
)
);

return $path;
}

Expand Down Expand Up @@ -193,16 +194,18 @@ protected function getTypeCheck()

/**
* @param JsonPointer $pointer
*
* @return string property path
*/
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
{
$result = array_map(
function($path) {
function ($path) {
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
},
$pointer->getPropertyPaths()
);

return trim(implode('', $result), '.');
}
}
6 changes: 4 additions & 2 deletions src/JsonSchema/Constraints/ConstraintInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,27 @@ public function addErrors(array $errors);
* @param JsonPointer|null $path
* @param string $message
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
* @param array $more more array elements to add to the error
* @param array $more more array elements to add to the error
*/
public function addError(JsonPointer $path = null, $message, $constraint='', array $more = null);

/**
* checks if the validator has not raised errors
*
* @return boolean
* @return bool
*/
public function isValid();

/**
* invokes the validation of an element
*
* @abstract
*
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
*
* @throws \JsonSchema\Exception\ExceptionInterface
*/
public function check(&$value, $schema = null, JsonPointer $path = null, $i = null);
Expand Down
11 changes: 6 additions & 5 deletions src/JsonSchema/Constraints/EnumConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

namespace JsonSchema\Constraints;

use JsonSchema\Entity\JsonPointer;

/**
Expand All @@ -19,7 +20,7 @@
class EnumConstraint extends Constraint
{
/**
* {@inheritDoc}
* {@inheritdoc}
*/
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
{
Expand All @@ -31,14 +32,14 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =

foreach ($schema->enum as $enum) {
$enumType = gettype($enum);
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == "array" && $enumType == "object") {
if ((object)$element == $enum) {
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
if ((object) $element == $enum) {
return;
}
}

if ($type === gettype($enum)) {
if ($type == "object") {
if ($type == 'object') {
if ($element == $enum) {
return;
}
Expand All @@ -48,6 +49,6 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
}
}

$this->addError($path, "Does not have a value in the enumeration " . json_encode($schema->enum), 'enum', array('enum' => $schema->enum,));
$this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array('enum' => $schema->enum));
}
}
23 changes: 14 additions & 9 deletions src/JsonSchema/Constraints/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Factory
protected $schemaStorage;

/**
* @var UriRetriever $uriRetriever
* @var UriRetriever
*/
protected $uriRetriever;

Expand All @@ -42,7 +42,7 @@ class Factory
private $typeCheck = array();

/**
* @var array $constraintMap
* @var array
*/
protected $constraintMap = array(
'array' => 'JsonSchema\Constraints\CollectionConstraint',
Expand All @@ -64,9 +64,9 @@ class Factory
private $instanceCache = array();

/**
* @param SchemaStorage $schemaStorage
* @param SchemaStorage $schemaStorage
* @param UriRetrieverInterface $uriRetriever
* @param int $checkMode
* @param int $checkMode
*/
public function __construct(
SchemaStorageInterface $schemaStorage = null,
Expand All @@ -76,7 +76,7 @@ public function __construct(
// set provided config options
$this->setConfig($checkMode);

$this->uriRetriever = $uriRetriever ?: new UriRetriever;
$this->uriRetriever = $uriRetriever ?: new UriRetriever();
$this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever);
}

Expand Down Expand Up @@ -122,7 +122,8 @@ public function getConfig($options = null)
if ($options === null) {
return $this->checkMode;
}
return ($this->checkMode & $options);

return $this->checkMode & $options;
}

/**
Expand All @@ -142,8 +143,8 @@ public function getTypeCheck()
{
if (!isset($this->typeCheck[$this->checkMode])) {
$this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST)
? new TypeCheck\LooseTypeCheck
: new TypeCheck\StrictTypeCheck;
? new TypeCheck\LooseTypeCheck()
: new TypeCheck\StrictTypeCheck();
}

return $this->typeCheck[$this->checkMode];
Expand All @@ -152,6 +153,7 @@ public function getTypeCheck()
/**
* @param string $name
* @param string $class
*
* @return Factory
*/
public function setConstraintClass($name, $class)
Expand All @@ -165,15 +167,18 @@ public function setConstraintClass($name, $class)
throw new InvalidArgumentException('Invalid class ' . $name);
}
$this->constraintMap[$name] = $class;

return $this;
}

/**
* Create a constraint instance for the given constraint name.
*
* @param string $constraintName
*
* @throws InvalidArgumentException if is not possible create the constraint instance
*
* @return ConstraintInterface|ObjectConstraint
* @throws InvalidArgumentException if is not possible create the constraint instance.
*/
public function createInstanceFor($constraintName)
{
Expand Down