Skip to content

Commit 42c1043

Browse files
alcoholbighappyface
authored andcommitted
Make code-style more consistent (#355)
* update branch-alias * add php-cs-fixer configuration * add php-cs-fixer to travis * apply code-style rules
1 parent 7eb38e2 commit 42c1043

File tree

67 files changed

+442
-334
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+442
-334
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ coverage
55
.buildpath
66
.project
77
.settings
8+
.php_cs
9+
.php_cs.cache
810
composer.lock
911
docs-api
1012
phpunit.xml

.php_cs.dist

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$finder = new PhpCsFixer\Finder();
4+
$config = new PhpCsFixer\Config('json-schema', 'json-schema style guide');
5+
$finder->in(__DIR__);
6+
7+
/* Based on ^2.1 of php-cs-fixer */
8+
$config
9+
->setRules(array(
10+
// default
11+
'@PSR2' => true,
12+
'@Symfony' => true,
13+
// additionally
14+
'array_syntax' => array('syntax' => 'long'),
15+
'binary_operator_spaces' => false,
16+
'concat_space' => array('spacing' => 'one'),
17+
'no_unused_imports' => false,
18+
'no_useless_else' => true,
19+
'no_useless_return' => true,
20+
'ordered_imports' => true,
21+
'phpdoc_no_package' => false,
22+
'phpdoc_order' => true,
23+
'phpdoc_summary' => false,
24+
'pre_increment' => false,
25+
'trailing_comma_in_multiline_array' => false,
26+
'simplified_null_return' => false,
27+
))
28+
->setFinder($finder)
29+
;
30+
31+
return $config;

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ language: php
44
cache:
55
directories:
66
- $HOME/.composer/cache
7+
- $HOME/.phpcsfixer
78

89
matrix:
910
fast_finish: true
@@ -13,7 +14,7 @@ matrix:
1314
- php: 5.5
1415
- php: 5.6
1516
- php: 7.0
16-
env: WITH_COVERAGE=true
17+
env: WITH_COVERAGE=true WITH_PHPCSFIXER=true
1718
- php: 7.1
1819
- php: 'nightly'
1920
- php: hhvm
@@ -29,3 +30,4 @@ install:
2930

3031
script:
3132
- if [[ "$WITH_COVERAGE" == "true" ]]; then ./vendor/bin/phpunit --coverage-text; else composer test; fi
33+
- 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

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@
5252
"bin": ["bin/validate-json"],
5353
"extra": {
5454
"branch-alias": {
55-
"dev-master": "4.0.x-dev"
55+
"dev-master": "5.0.x-dev"
5656
}
5757
},
5858
"scripts": {
5959
"test" : "vendor/bin/phpunit",
6060
"testOnly" : "vendor/bin/phpunit --colors --filter",
61-
"coverage" : "vendor/bin/phpunit --coverage-text"
61+
"coverage" : "vendor/bin/phpunit --coverage-text"
6262
}
6363
}

demo/demo.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php
2-
require(__DIR__ . '/../vendor/autoload.php');
2+
3+
require __DIR__ . '/../vendor/autoload.php';
34

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

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

1011
if ($validator->isValid()) {
1112
echo "The supplied JSON validates against the schema.\n";

src/JsonSchema/Constraints/BaseConstraint.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ class BaseConstraint
3333
*/
3434
public function __construct(Factory $factory = null)
3535
{
36-
$this->factory = $factory ? : new Factory();
36+
$this->factory = $factory ?: new Factory();
3737
}
3838

39-
/**
40-
* {@inheritDoc}
41-
*/
4239
public function addError(JsonPointer $path = null, $message, $constraint = '', array $more = null)
4340
{
4441
$error = array(
@@ -49,38 +46,28 @@ public function addError(JsonPointer $path = null, $message, $constraint = '', a
4946
);
5047

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

55-
if (is_array($more) && count($more) > 0)
56-
{
52+
if (is_array($more) && count($more) > 0) {
5753
$error += $more;
5854
}
5955

6056
$this->errors[] = $error;
6157
}
6258

63-
/**
64-
* {@inheritDoc}
65-
*/
6659
public function addErrors(array $errors)
6760
{
6861
if ($errors) {
6962
$this->errors = array_merge($this->errors, $errors);
7063
}
7164
}
7265

73-
/**
74-
* {@inheritDoc}
75-
*/
7666
public function getErrors()
7767
{
7868
return $this->errors;
7969
}
8070

81-
/**
82-
* {@inheritDoc}
83-
*/
8471
public function isValid()
8572
{
8673
return !$this->getErrors();

src/JsonSchema/Constraints/CollectionConstraint.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,31 @@
1919
*/
2020
class CollectionConstraint extends Constraint
2121
{
22-
2322
/**
24-
* {@inheritDoc}
23+
* {@inheritdoc}
2524
*/
2625
public function check(&$value, $schema = null, JsonPointer $path = null, $i = null)
2726
{
2827
// Verify minItems
2928
if (isset($schema->minItems) && count($value) < $schema->minItems) {
30-
$this->addError($path, "There must be a minimum of " . $schema->minItems . " items in the array", 'minItems', array('minItems' => $schema->minItems,));
29+
$this->addError($path, 'There must be a minimum of ' . $schema->minItems . ' items in the array', 'minItems', array('minItems' => $schema->minItems));
3130
}
3231

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

3837
// Verify uniqueItems
3938
if (isset($schema->uniqueItems) && $schema->uniqueItems) {
4039
$unique = $value;
4140
if (is_array($value) && count($value)) {
42-
$unique = array_map(function($e) { return var_export($e, true); }, $value);
41+
$unique = array_map(function ($e) {
42+
return var_export($e, true);
43+
}, $value);
4344
}
4445
if (count(array_unique($unique)) != count($value)) {
45-
$this->addError($path, "There are no duplicates allowed in the array", 'uniqueItems');
46+
$this->addError($path, 'There are no duplicates allowed in the array', 'uniqueItems');
4647
}
4748
}
4849

@@ -123,7 +124,7 @@ protected function validateItems(&$value, $schema = null, JsonPointer $path = nu
123124
$this->checkUndefined($v, $schema->additionalItems, $path, $k);
124125
} else {
125126
$this->addError(
126-
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems,));
127+
$path, 'The item ' . $i . '[' . $k . '] is not defined and the definition does not allow additional items', 'additionalItems', array('additionalItems' => $schema->additionalItems));
127128
}
128129
} else {
129130
// Should be valid against an empty schema

src/JsonSchema/Constraints/Constraint.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace JsonSchema\Constraints;
1111

12+
use JsonSchema\Entity\JsonPointer;
1213
use JsonSchema\SchemaStorage;
1314
use JsonSchema\Uri\UriRetriever;
1415
use JsonSchema\UriRetrieverInterface;
15-
use JsonSchema\Entity\JsonPointer;
1616

1717
/**
1818
* The Base Constraints, all Validators should extend this class
@@ -48,6 +48,7 @@ protected function incrementPath(JsonPointer $path = null, $i)
4848
array_filter(array($i), 'strlen')
4949
)
5050
);
51+
5152
return $path;
5253
}
5354

@@ -193,16 +194,18 @@ protected function getTypeCheck()
193194

194195
/**
195196
* @param JsonPointer $pointer
197+
*
196198
* @return string property path
197199
*/
198200
protected function convertJsonPointerIntoPropertyPath(JsonPointer $pointer)
199201
{
200202
$result = array_map(
201-
function($path) {
203+
function ($path) {
202204
return sprintf(is_numeric($path) ? '[%d]' : '.%s', $path);
203205
},
204206
$pointer->getPropertyPaths()
205207
);
208+
206209
return trim(implode('', $result), '.');
207210
}
208211
}

src/JsonSchema/Constraints/ConstraintInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,27 @@ public function addErrors(array $errors);
3838
* @param JsonPointer|null $path
3939
* @param string $message
4040
* @param string $constraint the constraint/rule that is broken, e.g.: 'minLength'
41-
* @param array $more more array elements to add to the error
41+
* @param array $more more array elements to add to the error
4242
*/
4343
public function addError(JsonPointer $path = null, $message, $constraint='', array $more = null);
4444

4545
/**
4646
* checks if the validator has not raised errors
4747
*
48-
* @return boolean
48+
* @return bool
4949
*/
5050
public function isValid();
5151

5252
/**
5353
* invokes the validation of an element
5454
*
5555
* @abstract
56+
*
5657
* @param mixed $value
5758
* @param mixed $schema
5859
* @param JsonPointer|null $path
5960
* @param mixed $i
61+
*
6062
* @throws \JsonSchema\Exception\ExceptionInterface
6163
*/
6264
public function check(&$value, $schema = null, JsonPointer $path = null, $i = null);

src/JsonSchema/Constraints/EnumConstraint.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
namespace JsonSchema\Constraints;
11+
1112
use JsonSchema\Entity\JsonPointer;
1213

1314
/**
@@ -19,7 +20,7 @@
1920
class EnumConstraint extends Constraint
2021
{
2122
/**
22-
* {@inheritDoc}
23+
* {@inheritdoc}
2324
*/
2425
public function check(&$element, $schema = null, JsonPointer $path = null, $i = null)
2526
{
@@ -31,14 +32,14 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
3132

3233
foreach ($schema->enum as $enum) {
3334
$enumType = gettype($enum);
34-
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == "array" && $enumType == "object") {
35-
if ((object)$element == $enum) {
35+
if ($this->factory->getConfig(self::CHECK_MODE_TYPE_CAST) && $type == 'array' && $enumType == 'object') {
36+
if ((object) $element == $enum) {
3637
return;
3738
}
3839
}
3940

4041
if ($type === gettype($enum)) {
41-
if ($type == "object") {
42+
if ($type == 'object') {
4243
if ($element == $enum) {
4344
return;
4445
}
@@ -48,6 +49,6 @@ public function check(&$element, $schema = null, JsonPointer $path = null, $i =
4849
}
4950
}
5051

51-
$this->addError($path, "Does not have a value in the enumeration " . json_encode($schema->enum), 'enum', array('enum' => $schema->enum,));
52+
$this->addError($path, 'Does not have a value in the enumeration ' . json_encode($schema->enum), 'enum', array('enum' => $schema->enum));
5253
}
5354
}

src/JsonSchema/Constraints/Factory.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Factory
2727
protected $schemaStorage;
2828

2929
/**
30-
* @var UriRetriever $uriRetriever
30+
* @var UriRetriever
3131
*/
3232
protected $uriRetriever;
3333

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

4444
/**
45-
* @var array $constraintMap
45+
* @var array
4646
*/
4747
protected $constraintMap = array(
4848
'array' => 'JsonSchema\Constraints\CollectionConstraint',
@@ -64,9 +64,9 @@ class Factory
6464
private $instanceCache = array();
6565

6666
/**
67-
* @param SchemaStorage $schemaStorage
67+
* @param SchemaStorage $schemaStorage
6868
* @param UriRetrieverInterface $uriRetriever
69-
* @param int $checkMode
69+
* @param int $checkMode
7070
*/
7171
public function __construct(
7272
SchemaStorageInterface $schemaStorage = null,
@@ -76,7 +76,7 @@ public function __construct(
7676
// set provided config options
7777
$this->setConfig($checkMode);
7878

79-
$this->uriRetriever = $uriRetriever ?: new UriRetriever;
79+
$this->uriRetriever = $uriRetriever ?: new UriRetriever();
8080
$this->schemaStorage = $schemaStorage ?: new SchemaStorage($this->uriRetriever);
8181
}
8282

@@ -122,7 +122,8 @@ public function getConfig($options = null)
122122
if ($options === null) {
123123
return $this->checkMode;
124124
}
125-
return ($this->checkMode & $options);
125+
126+
return $this->checkMode & $options;
126127
}
127128

128129
/**
@@ -142,8 +143,8 @@ public function getTypeCheck()
142143
{
143144
if (!isset($this->typeCheck[$this->checkMode])) {
144145
$this->typeCheck[$this->checkMode] = ($this->checkMode & Constraint::CHECK_MODE_TYPE_CAST)
145-
? new TypeCheck\LooseTypeCheck
146-
: new TypeCheck\StrictTypeCheck;
146+
? new TypeCheck\LooseTypeCheck()
147+
: new TypeCheck\StrictTypeCheck();
147148
}
148149

149150
return $this->typeCheck[$this->checkMode];
@@ -152,6 +153,7 @@ public function getTypeCheck()
152153
/**
153154
* @param string $name
154155
* @param string $class
156+
*
155157
* @return Factory
156158
*/
157159
public function setConstraintClass($name, $class)
@@ -165,15 +167,18 @@ public function setConstraintClass($name, $class)
165167
throw new InvalidArgumentException('Invalid class ' . $name);
166168
}
167169
$this->constraintMap[$name] = $class;
170+
168171
return $this;
169172
}
170173

171174
/**
172175
* Create a constraint instance for the given constraint name.
173176
*
174177
* @param string $constraintName
178+
*
179+
* @throws InvalidArgumentException if is not possible create the constraint instance
180+
*
175181
* @return ConstraintInterface|ObjectConstraint
176-
* @throws InvalidArgumentException if is not possible create the constraint instance.
177182
*/
178183
public function createInstanceFor($constraintName)
179184
{

0 commit comments

Comments
 (0)