Skip to content

Commit

Permalink
Tweak psalm config and make it happy
Browse files Browse the repository at this point in the history
  • Loading branch information
theseer committed Feb 23, 2021
1 parent 97f0a64 commit f336836
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

<projectFiles>
<directory name="src" />
<ignoreFiles>
<file name="src/autoload.php" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
Expand Down
7 changes: 2 additions & 5 deletions src/PreReleaseSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public function isGreaterThan(PreReleaseSuffix $suffix): bool {
return $this->getNumber() > $suffix->getNumber();
}

/**
* @param $value
*/
private function mapValueToScore($value): int {
private function mapValueToScore(string $value): int {
$value = \strtolower($value);

if (\array_key_exists($value, self::valueScoreMap)) {
Expand All @@ -69,7 +66,7 @@ private function mapValueToScore($value): int {
return 0;
}

private function parseValue($value): void {
private function parseValue(string $value): void {
$regex = '/-?((dev|beta|b|rc|alpha|a|patch|p)\.?(\d*)).*$/i';

if (\preg_match($regex, $value, $matches) !== 1) {
Expand Down
22 changes: 14 additions & 8 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,19 @@ class Version {
/** @var VersionNumber */
private $patch;

/** @var PreReleaseSuffix */
/** @var null|PreReleaseSuffix */
private $preReleaseSuffix;

/**
* @param string $versionString
*/
public function __construct($versionString) {
public function __construct(string $versionString) {
$this->ensureVersionStringIsValid($versionString);
$this->originalVersionString = $versionString;
}

public function getPreReleaseSuffix(): PreReleaseSuffix {
if ($this->preReleaseSuffix === null) {
throw new NoPreReleaseSuffixException('No pre-release suffix set');
}

return $this->preReleaseSuffix;
}

Expand All @@ -44,9 +45,9 @@ public function getOriginalString(): string {
public function getVersionString(): string {
$str = \sprintf(
'%d.%d.%d',
$this->getMajor()->getValue(),
$this->getMinor()->getValue(),
$this->getPatch()->getValue()
$this->getMajor()->getValue() ?? 0,
$this->getMinor()->getValue() ?? 0,
$this->getPatch()->getValue() ?? 0
);

if (!$this->hasPreReleaseSuffix()) {
Expand Down Expand Up @@ -116,6 +117,11 @@ public function getPatch(): VersionNumber {
return $this->patch;
}

/**
* @param string[] $matches
*
* @throws InvalidPreReleaseSuffixException
*/
private function parseVersion(array $matches): void {
$this->major = new VersionNumber((int)$matches['Major']);
$this->minor = new VersionNumber((int)$matches['Minor']);
Expand Down
19 changes: 11 additions & 8 deletions src/VersionConstraintParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ public function parse(string $value): VersionConstraint {
if ($constraint->getMinor()->isAny()) {
return new SpecificMajorVersionConstraint(
$constraint->getVersionString(),
$constraint->getMajor()->getValue()
$constraint->getMajor()->getValue() ?? 0
);
}

if ($constraint->getPatch()->isAny()) {
return new SpecificMajorAndMinorVersionConstraint(
$constraint->getVersionString(),
$constraint->getMajor()->getValue(),
$constraint->getMinor()->getValue()
$constraint->getMajor()->getValue() ?? 0,
$constraint->getMinor()->getValue() ?? 0
);
}

Expand Down Expand Up @@ -79,8 +79,8 @@ private function handleTildeOperator(string $value): AndVersionConstraintGroup {
),
new SpecificMajorAndMinorVersionConstraint(
$value,
$constraintValue->getMajor()->getValue(),
$constraintValue->getMinor()->getValue()
$constraintValue->getMajor()->getValue() ?? 0,
$constraintValue->getMinor()->getValue() ?? 0
)
];

Expand All @@ -97,11 +97,14 @@ private function handleCaretOperator(string $value): AndVersionConstraintGroup {
if ($constraintValue->getMajor()->getValue() === 0) {
$constraints[] = new SpecificMajorAndMinorVersionConstraint(
$value,
$constraintValue->getMajor()->getValue(),
$constraintValue->getMinor()->getValue()
$constraintValue->getMajor()->getValue() ?? 0,
$constraintValue->getMinor()->getValue() ?? 0
);
} else {
$constraints[] = new SpecificMajorVersionConstraint($value, $constraintValue->getMajor()->getValue());
$constraints[] = new SpecificMajorVersionConstraint(
$value,
$constraintValue->getMajor()->getValue() ?? 0
);
}

return new AndVersionConstraintGroup(
Expand Down
22 changes: 5 additions & 17 deletions src/VersionConstraintValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class VersionConstraintValue {
/** @var string */
private $versionString = '';

/**
* @param string $versionString
*/
public function __construct($versionString) {
public function __construct(string $versionString) {
$this->versionString = $versionString;

$this->parseVersion($versionString);
Expand Down Expand Up @@ -53,10 +50,7 @@ public function getPatch(): VersionNumber {
return $this->patch;
}

/**
* @param $versionString
*/
private function parseVersion($versionString): void {
private function parseVersion(string $versionString): void {
$this->extractBuildMetaData($versionString);
$this->extractLabel($versionString);
$this->stripPotentialVPrefix($versionString);
Expand All @@ -71,27 +65,21 @@ private function parseVersion($versionString): void {
$this->patch = new VersionNumber($patchValue);
}

/**
* @param string $versionString
*/
private function extractBuildMetaData(&$versionString): void {
private function extractBuildMetaData(string &$versionString): void {
if (\preg_match('/\+(.*)/', $versionString, $matches) === 1) {
$this->buildMetaData = $matches[1];
$versionString = \str_replace($matches[0], '', $versionString);
}
}

/**
* @param string $versionString
*/
private function extractLabel(&$versionString): void {
private function extractLabel(string &$versionString): void {
if (\preg_match('/-(.*)/', $versionString, $matches) === 1) {
$this->label = $matches[1];
$versionString = \str_replace($matches[0], '', $versionString);
}
}

private function stripPotentialVPrefix(&$versionString): void {
private function stripPotentialVPrefix(string &$versionString): void {
if ($versionString[0] !== 'v') {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/exceptions/NoPreReleaseSuffixException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php declare(strict_types = 1);
namespace PharIo\Version;

class NoPreReleaseSuffixException extends \Exception implements Exception {
}

0 comments on commit f336836

Please sign in to comment.