Passed
Push — master ( 5dd2f3...e80b48 )
by Théo
02:17
created

PharInfo   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 32
dl 0
loc 81
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoot() 0 3 1
A retrieveCompressionCount() 0 35 5
A getVersion() 0 3 2
A __construct() 0 11 3
A getPhar() 0 3 1
A getNormalizedMetadata() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box\PharInfo;
16
17
use function array_flip;
18
use function KevinGH\Box\get_phar_compression_algorithms;
19
use Phar;
20
use PharData;
21
use PharFileInfo;
22
use RecursiveIteratorIterator;
23
use UnexpectedValueException;
24
25
final class PharInfo
26
{
27
    private static $ALGORITHMS;
28
29
    private $phar;
30
31
    public function __construct(string $pharFile)
32
    {
33
        if (null === self::$ALGORITHMS) {
34
            self::$ALGORITHMS = array_flip(get_phar_compression_algorithms());
35
            self::$ALGORITHMS[Phar::NONE] = 'None';
36
        }
37
38
        try {
39
            $this->phar = new Phar($pharFile);
40
        } catch (UnexpectedValueException $exception) {
41
            $this->phar = new PharData($pharFile);
42
        }
43
    }
44
45
    public function retrieveCompressionCount(): array
46
    {
47
        $count = array_fill_keys(
48
            self::$ALGORITHMS,
49
            0
50
        );
51
52
        if ($this->phar instanceof PharData) {
53
            $count[self::$ALGORITHMS[$this->phar->isCompressed()]] = 1;
54
55
            return $count;
56
        }
57
58
        $countFile = static function (array $count, PharFileInfo $file): array {
59
            if (false === $file->isCompressed()) {
60
                ++$count['None'];
61
62
                return $count;
63
            }
64
65
            foreach (self::$ALGORITHMS as $compressionAlgorithmCode => $compressionAlgorithmName) {
66
                if ($file->isCompressed($compressionAlgorithmCode)) {
67
                    ++$count[$compressionAlgorithmName];
68
69
                    return $count;
70
                }
71
            }
72
73
            return $count;
74
        };
75
76
        return array_reduce(
77
            iterator_to_array(new RecursiveIteratorIterator($this->phar), true),
78
            $countFile,
79
            $count
80
        );
81
    }
82
83
    /**
84
     * @return Phar|PharData
85
     */
86
    public function getPhar()
87
    {
88
        return $this->phar;
89
    }
90
91
    public function getRoot(): string
92
    {
93
        return 'phar://'.str_replace('\\', '/', realpath($this->phar->getPath())).'/';
94
    }
95
96
    public function getVersion(): string
97
    {
98
        return '' !== $this->phar->getVersion() ? $this->phar->getVersion() : 'No information found';
99
    }
100
101
    public function getNormalizedMetadata(): ?string
102
    {
103
        $metadata = var_export($this->phar->getMetadata(), true);
104
105
        return 'NULL' === $metadata ? null : $metadata;
106
    }
107
}
108