Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit fc25e8f
Author: Peter Csajtai <[email protected]>
Date:   Mon Apr 1 18:12:50 2024 +0200

    Update README.md

commit aa964fd
Author: Peter Csajtai <[email protected]>
Date:   Mon Apr 1 17:56:40 2024 +0200

    Update Version.php

commit 9e3303a
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 23:23:21 2024 +0200

    Update README.md

commit dda776a
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 22:29:48 2024 +0200

    Update ci.yml

commit f3f6319
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 22:26:31 2024 +0200

    Fix report errors

commit 2050543
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 22:23:05 2024 +0200

    Reformat, fix phpunit execution

commit 5b1a770
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 22:12:08 2024 +0200

    Update composer.json

commit 23e2e63
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 21:48:12 2024 +0200

    Add tests report

commit 9c90e15
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 21:38:38 2024 +0200

    Add sonarcloud analysis

commit eb9141a
Author: Peter Csajtai <[email protected]>
Date:   Sun Mar 31 21:15:45 2024 +0200

    Update to PHP 8
  • Loading branch information
z4kn4fein committed Apr 1, 2024
1 parent 0c08e4f commit 049a1d8
Show file tree
Hide file tree
Showing 27 changed files with 235 additions and 309 deletions.
23 changes: 14 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '7.1', '7.2', '7.3', '7.4', '8.0', '8.1' ]
php-versions: [ '8.1', '8.2', '8.3' ]
steps:
- uses: actions/checkout@v4

Expand Down Expand Up @@ -41,17 +41,18 @@ jobs:
- name: Execute tests
run: vendor/bin/phpunit


coverage:
analysis:
needs: [test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
extensions: xdebug

- name: Validate composer.json and composer.lock
Expand All @@ -71,10 +72,14 @@ jobs:
composer install --prefer-dist --no-progress --no-suggest
- name: Execute coverage
run: vendor/bin/phpunit --coverage-clover clover.xml
run: vendor/bin/phpunit --coverage-clover=coverage.xml --log-junit=tests.xml
env:
XDEBUG_MODE: coverage

- name: Upload coverage report
run: bash <(curl -s https://codecov.io/bash)
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

php-cs-fixer:
runs-on: ubuntu-latest
Expand All @@ -84,7 +89,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
ini-values: 'memory_limit=-1'

- name: Validate composer.json and composer.lock
Expand Down Expand Up @@ -114,7 +119,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
ini-values: 'memory_limit=-1'

- name: Validate composer.json and composer.lock
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# php-semver
[![Build Status](https://github.com/z4kn4fein/php-semver/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/z4kn4fein/php-semver/actions/workflows/ci.yml)
[![Coverage Status](https://img.shields.io/codecov/c/github/z4kn4fein/php-semver.svg)](https://codecov.io/gh/z4kn4fein/php-semver)
[![Latest Stable Version](https://poser.pugx.org/z4kn4fein/php-semver/version)](https://packagist.org/packages/z4kn4fein/php-semver)
[![Total Downloads](https://poser.pugx.org/z4kn4fein/php-semver/downloads)](https://packagist.org/packages/z4kn4fein/php-semver)
[![Latest Unstable Version](https://poser.pugx.org/z4kn4fein/php-semver/v/unstable)](https://packagist.org/packages/z4kn4fein/php-semver)
[![Sonar Quality Gate](https://img.shields.io/sonar/quality_gate/z4kn4fein_php-semver?logo=sonarcloud&server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/project/overview?id=z4kn4fein_php-semver)
[![Sonar Coverage](https://img.shields.io/sonar/coverage/z4kn4fein_php-semver?logo=SonarCloud&server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/project/overview?id=z4kn4fein_php-semver)

Semantic Versioning library for PHP. It implements the full [semantic version 2.0.0](https://semver.org/spec/v2.0.0.html) specification and
provides ability to **parse**, **compare**, and **increment** semantic versions along with validation against **constraints**.

## Requirements
[PHP](https://www.php.net/) >= 7.1
| Version | PHP Version |
|---------------|-------------|
| `>=1.0, <1.2` | >=5.5 |
| `>=1.2, <3.0` | >=7.1 |
| `>=3.0` | >=8.1 |


## Install with [Composer](https://getcomposer.org/)
```shell
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
}
],
"require": {
"php": ">=7.1"
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^7|^8",
"phpunit/phpunit": "^10",
"phpstan/phpstan": "^1.0",
"friendsofphp/php-cs-fixer": "^3.0"
},
Expand Down
36 changes: 13 additions & 23 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutOutputDuringTests="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Unit Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
7 changes: 7 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sonar.projectKey=z4kn4fein_php-semver
sonar.organization=z4kn4fein
sonar.sources=src
sonar.tests=tests

sonar.php.tests.reportPath=tests.xml
sonar.php.coverage.reportPaths=coverage.xml
75 changes: 22 additions & 53 deletions src/Constraints/Condition.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace z4kn4fein\SemVer\Constraints;

use z4kn4fein\SemVer\SemverException;
Expand All @@ -13,11 +15,8 @@ class Condition implements VersionComparator
{
use Singles;

/** @var string */
private $operator;

/** @var Version */
private $version;
private string $operator;
private Version $version;

public function __construct(string $operator, Version $version)
{
Expand All @@ -38,61 +37,31 @@ public function __toString(): string
*/
public function isSatisfiedBy(Version $version): bool
{
switch ($this->operator) {
case Op::EQUAL:
return $version->isEqual($this->version);

case Op::NOT_EQUAL:
return !$version->isEqual($this->version);

case Op::LESS_THAN:
return $version->isLessThan($this->version);

case Op::LESS_THAN_OR_EQUAL:
case Op::LESS_THAN_OR_EQUAL2:
return $version->isLessThanOrEqual($this->version);

case Op::GREATER_THAN:
return $version->isGreaterThan($this->version);

case Op::GREATER_THAN_OR_EQUAL:
case Op::GREATER_THAN_OR_EQUAL2:
return $version->isGreaterThanOrEqual($this->version);

default:
throw new SemverException(sprintf('Invalid operator in condition %s', (string) $this));
}
return match ($this->operator) {
Op::EQUAL => $version->isEqual($this->version),
Op::NOT_EQUAL => !$version->isEqual($this->version),
Op::LESS_THAN => $version->isLessThan($this->version),
Op::LESS_THAN_OR_EQUAL, Op::LESS_THAN_OR_EQUAL2 => $version->isLessThanOrEqual($this->version),
Op::GREATER_THAN => $version->isGreaterThan($this->version),
Op::GREATER_THAN_OR_EQUAL, Op::GREATER_THAN_OR_EQUAL2 => $version->isGreaterThanOrEqual($this->version),
default => throw new SemverException(sprintf('Invalid operator in condition %s', $this)),
};
}

/**
* @throws SemverException
*/
public function opposite(): string
{
switch ($this->operator) {
case Op::EQUAL:
return Op::NOT_EQUAL.$this->version;

case Op::NOT_EQUAL:
return Op::EQUAL.$this->version;

case Op::LESS_THAN:
return Op::GREATER_THAN_OR_EQUAL.$this->version;

case Op::LESS_THAN_OR_EQUAL:
case Op::LESS_THAN_OR_EQUAL2:
return Op::GREATER_THAN.$this->version;

case Op::GREATER_THAN:
return Op::LESS_THAN_OR_EQUAL.$this->version;

case Op::GREATER_THAN_OR_EQUAL:
case Op::GREATER_THAN_OR_EQUAL2:
return Op::LESS_THAN.$this->version;

default:
throw new SemverException(sprintf('Invalid operator in condition %s', (string) $this));
}
return match ($this->operator) {
Op::EQUAL => Op::NOT_EQUAL.$this->version,
Op::NOT_EQUAL => Op::EQUAL.$this->version,
Op::LESS_THAN => Op::GREATER_THAN_OR_EQUAL.$this->version,
Op::LESS_THAN_OR_EQUAL, Op::LESS_THAN_OR_EQUAL2 => Op::GREATER_THAN.$this->version,
Op::GREATER_THAN => Op::LESS_THAN_OR_EQUAL.$this->version,
Op::GREATER_THAN_OR_EQUAL, Op::GREATER_THAN_OR_EQUAL2 => Op::LESS_THAN.$this->version,
default => throw new SemverException(sprintf('Invalid operator in condition %s', $this)),
};
}

public static function greaterThanMin(): Condition
Expand Down
14 changes: 8 additions & 6 deletions src/Constraints/Constraint.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace z4kn4fein\SemVer\Constraints;

use Exception;
Expand All @@ -21,7 +23,7 @@ class Constraint
use Validator;

/** @var VersionComparator[][] */
private $comparators;
private array $comparators;

/**
* @param VersionComparator[][] $comparators
Expand Down Expand Up @@ -82,7 +84,7 @@ public static function parseOrNull(string $constraintString): ?Constraint
{
try {
return self::parse($constraintString);
} catch (Exception $exception) {
} catch (Exception) {
return null;
}
}
Expand All @@ -92,9 +94,9 @@ public static function parseOrNull(string $constraintString): ?Constraint
*
* @param string $constraintString the string to parse
*
* @throws SemverException when the constraint string is invalid
*
* @return Constraint the parsed constraint
*
* @throws SemverException when the constraint string is invalid
*/
public static function parse(string $constraintString): Constraint
{
Expand Down Expand Up @@ -152,7 +154,7 @@ function ($matches) use (&$result) {
}

/**
* @param mixed[] $matches
* @param string[] $matches
*
* @throws SemverException
*/
Expand Down Expand Up @@ -181,7 +183,7 @@ private static function hyphenToComparator(array $matches): VersionComparator
}

/**
* @param mixed[] $matches
* @param string[] $matches
*
* @throws SemverException
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Constraints/Op.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace z4kn4fein\SemVer\Constraints;

/**
Expand Down
Loading

0 comments on commit 049a1d8

Please sign in to comment.