Skip to content

Commit c083fc1

Browse files
committed
PHPStan type hints and fixes.
1 parent 8b0607f commit c083fc1

6 files changed

Lines changed: 31 additions & 13 deletions

File tree

src/Parser/ComponentDetector.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function parseBranchOnly() : void
6565
$matches
6666
);
6767

68-
if(empty($matches[0])) {
68+
if(empty($matches[1])) {
6969
return;
7070
}
7171

@@ -90,9 +90,9 @@ private function parseBranchPrepend() : bool
9090
}
9191

9292
$matches = $this->nullifyEmpty($matches);
93-
$tag = $matches[2] ?? $matches[5];
93+
$tag = $matches[2] ?? $matches[5] ?? '';
9494
$number = $matches[3] ?? 0;
95-
$branch = $this->detectOriginalBranch($matches[1] ?? $matches[4]);
95+
$branch = $this->detectOriginalBranch($matches[1] ?? $matches[4] ?? '');
9696

9797
$this->tag = new VersionTag(
9898
$this->version,
@@ -138,10 +138,10 @@ private function parseTagSolo(bool $anywhere=false) : bool
138138
}
139139

140140
$matches = $this->nullifyEmpty($matches);
141-
$tag = $matches[1] ?? $matches[3];
141+
$tag = $matches[1] ?? $matches[3] ?? '';
142142
$number = $matches[2] ?? 0;
143143

144-
$branch = str_replace($matches[0], '-', $this->tagParts);
144+
$branch = str_replace((string)$matches[0], '-', $this->tagParts);
145145

146146
while(strpos($branch, '--') !== false) {
147147
$branch = str_replace('--', '-', $branch);
@@ -172,9 +172,9 @@ private function parseBranchAppend() : bool
172172
}
173173

174174
$matches = $this->nullifyEmpty($matches);
175-
$tag = $matches[1] ?? $matches[4];
175+
$tag = $matches[1] ?? $matches[4] ?? '';
176176
$number = $matches[2] ?? 0;
177-
$branch = $matches[3] ?? $matches[5];
177+
$branch = $matches[3] ?? $matches[5] ?? '';
178178

179179
$this->tag = new VersionTag(
180180
$this->version,
@@ -186,6 +186,10 @@ private function parseBranchAppend() : bool
186186
return true;
187187
}
188188

189+
/**
190+
* @param array<int,string> $values
191+
* @return array<int,string|null>
192+
*/
189193
private function nullifyEmpty(array $values) : array
190194
{
191195
foreach($values as $idx => $value) {

src/Parser/ShortTags.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ class ShortTags
1616
VersionParser::TAG_TYPE_DEV => VersionParser::TAG_TYPE_DEV_SHORT
1717
);
1818

19+
/**
20+
* @var array<string,string>|null
21+
*/
1922
private static ?array $shortTags = null;
2023

24+
/**
25+
* @return array<string,string>
26+
*/
2127
public static function getTagNames() : array
2228
{
2329
if(!isset(self::$shortTags)) {

src/Parser/TagWeights.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class TagWeights
2626
VersionParser::TAG_TYPE_DEV_SHORT => 8
2727
);
2828

29+
/**
30+
* @var array<string,int>|null
31+
*/
2932
private static ?array $tagWeights = null;
3033

3134
/**

src/VersionParser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,6 @@ class VersionParser implements Stringable
7777
private string $separator = '-';
7878
private bool $lowercase = true;
7979

80-
private static ?array $tagWeights = null;
81-
82-
private static ?array $shortTags = null;
83-
8480
private string $original;
8581

8682
private string $tagParts = '';
@@ -263,7 +259,7 @@ public function getTag() : string
263259
return '';
264260
}
265261

266-
public function getTagInfo() : VersionTag
262+
public function getTagInfo() : ?VersionTag
267263
{
268264
return $this->tag;
269265
}
@@ -418,6 +414,9 @@ private function parseTag() : void
418414
$this->tag = (new ComponentDetector($this, $this->tagParts))->detectTag();
419415
}
420416

417+
/**
418+
* @return array{originalVersion:string,normalized:string,majorVersion:int,minorVersion:int,patchVersion:int,shortVersion:string,buildNumber:float,buildNumberInt:int,tag:array<string,mixed>|null}
419+
*/
421420
public function toArray() : array
422421
{
423422
$tag = null;

src/VersionTag.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,15 @@ public function isTagType(string $type) : bool
158158
return $this->getTagType() === $type || $this->getTagName() === $type;
159159
}
160160

161+
/**
162+
* @return array{tagName:string, tagType:string, number:int, branch:string, weight:int, normalized:string}
163+
*/
161164
public function toArray() : array
162165
{
163166
return array(
164167
'tagName' => $this->getTagName(),
165168
'tagType' => $this->getTagType(),
166-
'number' => $this->getTagName(),
169+
'number' => $this->getNumber(),
167170
'branch' => $this->getBranchName(),
168171
'weight' => $this->getWeight(),
169172
'normalized' => $this->getNormalized()

tests/phpstan/output.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
[OK] No errors
3+

0 commit comments

Comments
 (0)