Skip to content

Commit 9de2244

Browse files
committed
Fix PHPStan error
1 parent 9b57fff commit 9de2244

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/Parser/Header.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,36 @@ public function getName(): string
8080
return rtrim($str, "\0");
8181
}
8282

83+
/**
84+
* @return int<1, max>
85+
*/
8386
public function getSize(): int
8487
{
8588
if (array_key_exists('size', $this->pax)) {
86-
return (int)$this->pax['size'];
89+
$size = (int)$this->pax['size'];
90+
} else {
91+
$size = rtrim(substr($this->content, 124, 12));
92+
if (preg_match('/^[0-7]+$/D', $size) !== 1) {
93+
throw new InvalidArchiveFormatException(
94+
sprintf(
95+
"Invalid Tar header format, file size must be octal number, '%s' got instead",
96+
$size
97+
)
98+
);
99+
}
100+
$size = (int)octdec($size);
87101
}
88102

89-
$str = rtrim(substr($this->content, 124, 12));
90-
if (preg_match('/^[0-7]+$/D', $str) !== 1) {
103+
if ($size < 1) {
91104
throw new InvalidArchiveFormatException(
92105
sprintf(
93-
"Invalid Tar header format, file size must be octal number, '%s' got instead",
94-
$str
106+
"Invalid Tar header format, file size must be positive number, '%s' got instead",
107+
$size
95108
)
96109
);
97110
}
98111

99-
return (int)octdec($str);
112+
return $size;
100113
}
101114

102115
public function mergePaxHeader(Header $header): void

0 commit comments

Comments
 (0)