Issues (224)

src/PharInfo/PharInfo.php (3 issues)

Labels
Severity
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 KevinGH\Box\Phar\CompressionAlgorithm;
18
use Phar;
19
use PharData;
20
use PharFileInfo;
21
use RecursiveIteratorIterator;
22
use UnexpectedValueException;
23
use function KevinGH\Box\unique_id;
24
use function realpath;
25
use function str_replace;
26
27
/**
28
 * @deprecated Deprecated since 4.4.1 in favour of \KevinGH\Box\Phar\PharInfo.
29
 */
30
final class PharInfo
31
{
32
    private static array $ALGORITHMS;
33
34
    private Phar|PharData $phar;
35
36
    private ?array $compressionCount = null;
37
    private ?string $hash = null;
38
39
    public function __construct(string $pharFile)
40
    {
41
        self::initAlgorithms();
42
43
        try {
44
            $this->phar = new Phar($pharFile);
45
        } catch (UnexpectedValueException) {
46
            $this->phar = new PharData($pharFile);
47
        }
48
    }
49
50
    private static function initAlgorithms(): void
51
    {
52
        if (!isset(self::$ALGORITHMS)) {
53
            self::$ALGORITHMS = [];
54
55
            foreach (CompressionAlgorithm::cases() as $compressionAlgorithm) {
56
                self::$ALGORITHMS[$compressionAlgorithm->value] = $compressionAlgorithm->name;
0 ignored issues
show
The property name does not seem to exist on KevinGH\Box\Phar\CompressionAlgorithm.
Loading history...
57
            }
58
        }
59
    }
60
61
    public function equals(self $pharInfo): bool
62
    {
63
        return
64
            $pharInfo->getCompressionCount() === $this->getCompressionCount()
65
            && $pharInfo->getNormalizedMetadata() === $this->getNormalizedMetadata();
66
    }
67
68
    public function getCompressionCount(): array
69
    {
70
        if (null === $this->compressionCount || $this->hash !== $this->getPharHash()) {
71
            $this->compressionCount = $this->calculateCompressionCount();
72
            $this->compressionCount['None'] = $this->compressionCount[CompressionAlgorithm::NONE->name];
0 ignored issues
show
The property name does not seem to exist on KevinGH\Box\Phar\CompressionAlgorithm.
Loading history...
73
            unset($this->compressionCount[CompressionAlgorithm::NONE->name]);
74
            $this->hash = $this->getPharHash();
75
        }
76
77
        return $this->compressionCount;
78
    }
79
80
    public function getPhar(): Phar|PharData
81
    {
82
        return $this->phar;
83
    }
84
85
    public function getRoot(): string
86
    {
87
        // Do not cache the result
88
        return 'phar://'.str_replace('\\', '/', realpath($this->phar->getPath())).'/';
89
    }
90
91
    public function getVersion(): string
92
    {
93
        // Do not cache the result
94
        return '' !== $this->phar->getVersion() ? $this->phar->getVersion() : 'No information found';
95
    }
96
97
    public function getNormalizedMetadata(): ?string
98
    {
99
        // Do not cache the result
100
        $metadata = var_export($this->phar->getMetadata(), true);
101
102
        return 'NULL' === $metadata ? null : $metadata;
103
    }
104
105
    private function getPharHash(): string
106
    {
107
        // If no signature is available (e.g. a tar.gz file), we generate a random hash to ensure
108
        // it will always be invalidated
109
        return $this->phar->getSignature()['hash'] ?? unique_id('');
110
    }
111
112
    private function calculateCompressionCount(): array
113
    {
114
        $count = array_fill_keys(
115
            self::$ALGORITHMS,
116
            0,
117
        );
118
119
        if ($this->phar instanceof PharData) {
120
            $count[self::$ALGORITHMS[$this->phar->isCompressed()]] = 1;
121
122
            return $count;
123
        }
124
125
        $countFile = static function (array $count, PharFileInfo $file): array {
126
            if (false === $file->isCompressed()) {
127
                ++$count[CompressionAlgorithm::NONE->name];
0 ignored issues
show
The property name does not seem to exist on KevinGH\Box\Phar\CompressionAlgorithm.
Loading history...
128
129
                return $count;
130
            }
131
132
            foreach (self::$ALGORITHMS as $compressionAlgorithmCode => $compressionAlgorithmName) {
133
                if ($file->isCompressed($compressionAlgorithmCode)) {
134
                    ++$count[$compressionAlgorithmName];
135
136
                    return $count;
137
                }
138
            }
139
140
            return $count;
141
        };
142
143
        return array_reduce(
144
            iterator_to_array(new RecursiveIteratorIterator($this->phar), true),
145
            $countFile,
146
            $count,
147
        );
148
    }
149
}
150