Passed
Push — master ( b3a69d...79ebcc )
by Allan
02:21 queued 11s
created

ApplierErrorFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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\Factories;
7
8
class ApplierErrorFactory
9
{
10
    /**
11
     * @var \Vaimo\ComposerPatches\Utils\DataUtils
12
     */
13
    private $dataUtils;
14
15
    public function __construct()
16
    {
17
        $this->dataUtils = new \Vaimo\ComposerPatches\Utils\DataUtils();
18
    }
19
20
    public function create(array $outputRecords)
21
    {
22
        $phrases = array(
23
            'failed',
24
            'unexpected',
25
            'malformed',
26
            'error',
27
            'corrupt',
28
            'can\'t find file',
29
            'patch unexpectedly ends',
30
            'due to output analysis',
31
            'no file to patch',
32
            'seem to find a patch in there anywhere',
33
            'Only garbage was found in the patch input',
34
            'patch fragment without header at line'
35
        );
36
37
        $messages = $this->collectErrors($outputRecords, $phrases);
38
        $failure = new \Vaimo\ComposerPatches\Exceptions\ApplierFailure();
39
        $failure->setErrors($messages);
40
41
        return $failure;
42
    }
43
44
    private function collectErrors(array $outputRecords, array $filters)
45
    {
46
        $pathMarker = '\|\+\+\+\s(?P<match>.*?)(\t|$)';
47
48
        $errorMatcher = sprintf(
49
            '/(%s)/i',
50
            implode('|', array_merge($filters, array($pathMarker)))
51
        );
52
53
        $pathMatcher = sprintf('/^%s/i', $pathMarker);
54
        $result = array();
55
56
        foreach ($outputRecords as $code => $output) {
57
            $lines = is_array($output) ? $output : explode(PHP_EOL, $output);
58
59
            $result[$code] = $this->dataUtils->listToGroups(
60
                preg_grep($errorMatcher, $lines),
61
                $pathMatcher
62
            );
63
        }
64
65
        return $result;
66
    }
67
}
68