Passed
Push — master ( 42c4d1...80535d )
by Allan
02:39 queued 12s
created

OutputGenerator::prioritizeErrors()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 1
dl 0
loc 33
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Console;
7
8
class OutputGenerator
9
{
10
    /**
11
     * @var \Vaimo\ComposerPatches\Logger
12
     */
13
    private $logger;
14
    
15
    public function __construct(
16
        \Vaimo\ComposerPatches\Logger $logger
17
    ) {
18
        $this->logger = $logger;
19
    }
20
21
    public function generateForException(\Exception $exception)
22
    {
23
        if ($exception instanceof \Vaimo\ComposerPatches\Exceptions\ApplierFailure) {
24
            $errors = array_filter($exception->getErrors());
25
            
26
            if (empty($errors)) {
27
                return;
28
            }
29
            
30
            $lines = $this->createOutputLines(
31
                $this->prioritizeErrors($errors)
32
            );
33
34
            $lines = array_merge(array('Probable causes for the failure:', ''), $lines);
35
36
            $lines = array_merge($lines, array(
37
                '',
38
                '(For full, unfiltered details please use: composer patch:apply -vvv)'
39
            ));
40
41
            $this->logger->writeNotice('warning', $lines);
42
        }
43
    }
44
    
45
    private function createOutputLines(array $errors)
46
    {
47
        $lines = array();
48
        
49
        foreach ($errors as $type => $groups) {
50
            if (!empty($lines)) {
51
                $lines[] = '';
52
            }
53
54
            $lines[] = sprintf('> %s', $type);
55
56
            foreach ($groups as $path => $messages) {
57
                $details = array(
58
                    '@' => $path,
59
                    '!' => reset($messages)
60
                );
61
62
                foreach ($details as $marker => $value) {
63
                    foreach (explode(PHP_EOL, $value) as $index => $item) {
64
                        $lines[] = sprintf('  %s %s', !$index ? $marker : ' ', $item);
65
                    }
66
                }
67
            }
68
        }
69
70
        return $lines;
71
    }
72
    
73
    private function prioritizeErrors(array $errors)
74
    {
75
        $filters = array(
76
            'can\'t find file',
77
            'unable to find file',
78
            'no such file',
79
            'no file to patch'
80
        );
81
82
        $filterPattern = sprintf('/(%s)/i', implode('|', $filters));
83
84
        $result = array();
85
86
        foreach ($errors as $code => $groups) {
87
            $result[$code] = array();
88
89
            foreach ($groups as $path => $messages) {
90
                $messages = array_unique($messages);
91
92
                $fileNotFoundMessages = preg_grep($filterPattern, $messages);
93
94
                if ($fileNotFoundMessages !== $messages) {
95
                    $messages = array_merge(
96
                        array_diff($messages, $fileNotFoundMessages),
97
                        $fileNotFoundMessages
98
                    );
99
                }
100
101
                $result[$code][$path] = $messages;
102
            }
103
        }
104
105
        return array_map('array_filter', $result);
106
    }
107
}
108