Passed
Push — master ( 11eaad...3f8f84 )
by Allan
02:05
created

OutputGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
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 = array();
31
32
            foreach ($this->prioritizeErrors($errors) as $type => $groups) {
33
                if (!empty($lines)) {
34
                    $lines[] = '';
35
                }
36
37
                $lines[] = sprintf('> %s', $type);
38
                
39
                foreach ($groups as $path => $messages) {
40
                    $lines[] = sprintf('  @ %s', $path);
41
                    $lines[] = sprintf('  ! %s', reset($messages));
42
                }
43
            }
44
45
            $lines = array_merge(array('Probable causes for the failure:', ''), $lines);
46
47
            $lines = array_merge($lines, array(
48
                '',
49
                '(For full, unfiltered details please use: composer patch:apply -vvv)'
50
            ));
51
52
            $this->logger->writeNotice('warning', $lines);
53
        }
54
    }
55
    
56
    private function prioritizeErrors(array $errors)
57
    {
58
        $filters = array(
59
            'can\'t find file',
60
            'unable to find file',
61
            'no such file',
62
            'no file to patch'
63
        );
64
65
        $filterPattern = sprintf('/(%s)/i', implode('|', $filters));
66
67
        $result = array();
68
69
        foreach ($errors as $code => $groups) {
70
            $result[$code] = array();
71
72
            foreach ($groups as $path => $messages) {
73
                $messages = array_unique($messages);
74
75
                $fileNotFoundMessages = preg_grep($filterPattern, $messages);
76
77
                if ($fileNotFoundMessages !== $messages) {
78
                    $messages = array_merge(
79
                        array_diff($messages, $fileNotFoundMessages),
80
                        $fileNotFoundMessages
81
                    );
82
                }
83
84
                $result[$code][$path] = $messages;
85
            }
86
        }
87
88
        return array_map('array_filter', $result);
89
    }
90
}
91