Passed
Push — master ( 71cd79...3a5308 )
by Vincent
02:53 queued 20s
created

HaveValidationService::succeed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiAssert;
6
7
use PHPUnit\Framework\Assert as PHPUnit;
8
use VGirol\JsonApiStructure\Exception\ValidationException;
9
use VGirol\JsonApiStructure\ValidateService;
10
11
/**
12
 * This trait add the ability to call ValidateService.
13
 */
14
trait HaveValidationService
15
{
16
    /**
17
     * Undocumented variable
18
     *
19
     * @var ValidateService
20
     */
21
    private static $service;
22
23
    /**
24
     * Undocumented function
25
     *
26
     * @return ValidateService
27
     */
28 735
    protected static function getServiceInstance(): ValidateService
29
    {
30 735
        if (static::$service === null) {
0 ignored issues
show
Bug introduced by
Since $service is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $service to at least protected.
Loading history...
31 3
            static::$service = static::createServiceInstance();
32
        }
33
34 735
        return static::$service;
35
    }
36
37
    /**
38
     * Undocumented function
39
     *
40
     * @param string $method
41
     * @param mixed ...$args
42
     *
43
     * @return mixed
44
     */
45 735
    protected static function proxyService(string $method, ...$args)
46
    {
47 735
        $service = static::getServiceInstance();
48
49 735
        return \call_user_func([$service, $method], ...$args);
50
    }
51
52
    /**
53
     * Undocumented function
54
     *
55
     * @param string $method
56
     * @param mixed ...$args
57
     *
58
     * @return void
59
     */
60 720
    protected static function askService(string $method, ...$args): void
61
    {
62
        try {
63 720
            static::proxyService($method, ...$args);
64 276
            static::succeed();
65 450
        } catch (ValidationException $e) {
66 408
            PHPUnit::fail($e->getMessage());
67
        }
68 276
    }
69
70
    /**
71
     * Undocumented function
72
     *
73
     * @param string $message
74
     *
75
     * @return void
76
     * @throws \PHPUnit\Framework\AssertionFailedError
77
     */
78 276
    protected static function succeed(string $message = ''): void
79
    {
80 276
        PHPUnit::assertTrue(true, $message);
81 276
    }
82
83
    /**
84
     * Undocumented function
85
     *
86
     * @return ValidateService
87
     */
88 3
    private static function createServiceInstance(): ValidateService
89
    {
90 3
        return new ValidateService();
91
    }
92
}
93