Passed
Push — master ( 5cd32c...1a1aa9 )
by Rafael
05:46
created

DeprecationAdviser::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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
        $message = '';
50
        if (!empty($this->warnings)) {
51
            uasort(
52
                $this->warnings,
53
                function ($a, $b) {
54
                    if (count($a) === count($b)) {
55
                        return 0;
56
                    }
57
58
                    return (count($a) > count($b)) ? -1 : 1;
59
                }
60
            );
61
62
            foreach ($this->warnings as $message => $warnings) {
63
                $count = count($warnings);
64
                echo sprintf("%sx: %s\n", $count, $message);
65
            }
66
        }
67
68
        return $message;
69
    }
70
}
71