GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Failed Conditions
Pull Request — master (#13)
by Enrico
09:04 queued 06:14
created

Assert::assertJsonMatchesSchemaString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1.008
1
<?php
2
3
/*
4
 * This file is part of the phpunit-json-assertions package.
5
 *
6
 * (c) Enrico Stahn <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace EnricoStahn\JsonAssert;
13
14
use JsonSchema\RefResolver;
15
use JsonSchema\Uri\UriRetriever;
16
use JsonSchema\Validator;
17
18
/**
19
 * Asserts to validate JSON data.
20
 *
21
 * - All assert methods expect deserialised JSON data (an actual object or array)
22
 *   since the deserialisation method should be up to the user.
23
 * - We provide a convenience method to transfer whatever into a JSON object (see ::getJsonObject(mixed))
24
 */
25
trait Assert
26
{
27
    /**
28
     * Asserts that json content is valid according to the provided schema file.
29
     *
30
     * Example:
31
     *
32
     *   static::assertJsonMatchesSchema('./schema.json', json_decode('{"foo":1}'))
33
     *
34
     * @param string       $schema  Path to the schema file
35
     * @param array|object $content JSON array or object
36
     */
37 9
    public static function assertJsonMatchesSchema($schema, $content)
38
    {
39 9
        $retriever = new UriRetriever();
40 9
        $schema = $retriever->retrieve('file://'.realpath($schema));
41
42
        // Assume references are relative to the current file
43
        // Create an issue or pull request if you need more complex use cases
44 9
        $refResolver = new RefResolver($retriever);
0 ignored issues
show
Bug introduced by
The call to RefResolver::__construct() misses a required argument $uriResolver.

This check looks for function calls that miss required arguments.

Loading history...
45
        $refResolver->resolve($schema, $schema->id);
0 ignored issues
show
Documentation introduced by
$schema is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
The call to RefResolver::resolve() has too many arguments starting with $schema->id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
46
47
        $validator = new Validator();
48
        $validator->check($content, $schema);
49
50
        $message = '- Property: %s, Contraint: %s, Message: %s';
51
        $messages = array_map(function ($exception) use ($message) {
52
            return sprintf($message, $exception['property'], $exception['constraint'], $exception['message']);
53
        }, $validator->getErrors());
54
        $messages[] = '- Response: '.json_encode($content);
55
56
        \PHPUnit_Framework_Assert::assertTrue($validator->isValid(), implode("\n", $messages));
57
    }
58
59
    /**
60
     * Asserts that json content is valid according to the provided schema string.
61
     *
62
     * @param string       $schema  Schema data
63
     * @param array|object $content JSON content
64
     */
65 2
    public static function assertJsonMatchesSchemaString($schema, $content)
66
    {
67 2
        $file = tempnam(sys_get_temp_dir(), 'json-schema-');
68 2
        file_put_contents($file, $schema);
69
70 2
        self::assertJsonMatchesSchema($file, $content);
71
    }
72
73
    /**
74
     * Asserts if the value retrieved with the expression equals the expected value.
75
     *
76
     * Example:
77
     *
78
     *     static::assertJsonValueEquals(33, 'foo.bar[0]', $json);
79
     *
80
     * @param mixed        $expected   Expected value
81
     * @param string       $expression Expression to retrieve the result
82
     *                                 (e.g. locations[?state == 'WA'].name | sort(@))
83
     * @param array|object $json       JSON Content
84
     */
85 4
    public static function assertJsonValueEquals($expected, $expression, $json)
86
    {
87 4
        $result = \JmesPath\Env::search($expression, $json);
88
89 4
        \PHPUnit_Framework_Assert::assertEquals($expected, $result);
90 3
        \PHPUnit_Framework_Assert::assertInternalType(gettype($expected), $result);
91 3
    }
92
93
    /**
94
     * Helper method to deserialise a JSON string into an object.
95
     *
96
     * @param mixed $data The JSON string
97
     *
98
     * @return array|object
99
     */
100 4
    public static function getJsonObject($data)
101
    {
102 4
        return (is_array($data) || is_object($data)) ? $data : json_decode($data);
103
    }
104
}
105