Completed
Push — master ( 59746f...828139 )
by Rafael
04:54
created

DeprecationAdviser::__toString()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 19
cp 0
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 12
nc 2
nop 0
crap 30
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Deprecation;
12
13
use Behat\Testwork\EventDispatcher\Event\SuiteTested;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
/**
17
 * Storage a set of deprecation warnings to display to users after finish all tests
18
 */
19
class DeprecationAdviser implements EventSubscriberInterface
20
{
21
    /**
22
     * @var array
23
     */
24
    protected $warnings = [];
25
26
    public static function getSubscribedEvents()
27
    {
28
        return [
29
            SuiteTested::AFTER => 'dump',
30
        ];
31
    }
32
33
    /**
34
     * @param string      $message
35
     * @param null|string $file
36
     * @param int|null    $line
37
     */
38
    public function addWarning(string $message, ?string $file = null, ?int $line = null)
39
    {
40
        $warning = new DeprecationWarning($message, $file, $line);
41
        $this->warnings[$warning->getMessage()][] = $warning;
42
    }
43
44
    /**
45
     * Dump all warning ordered by amount of times triggered
46
     */
47
    public function dump()
48
    {
49
        echo (string) $this;
50
    }
51
52
    public function __toString(): string
53
    {
54
        $message = '';
55
        if (!empty($this->warnings)) {
56
            uasort(
57
                $this->warnings,
58
                function ($a, $b) {
59
                    if (count($a) === count($b)) {
60
                        return 0;
61
                    }
62
63
                    return (count($a) > count($b)) ? -1 : 1;
64
                }
65
            );
66
67
            foreach ($this->warnings as $message => $warnings) {
68
                $count = count($warnings);
69
                $message .= sprintf("%sx: %s\n", $count, $message);
70
            }
71
        }
72
73
        return $message;
74
    }
75
}
76