Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump PHP version to > 8.1 #5

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.0', '8.1' ]
php-versions: [ '8.1', '8.2', '8.3' ]

name: PHP ${{ matrix.php-versions }}

Expand All @@ -25,9 +25,10 @@ jobs:
run: composer install --no-ansi --no-interaction --no-scripts --no-suggest --no-progress

- name: Execute tests (Unit and Feature tests) via PHPUnit
run: |
composer test
run: composer test:no-coverage

- name: Execute static analysis
run: |
composer analyse
run: composer analyse

- name: Execute coding standards check
run: composer lint:check
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
/.idea/
/build/
.phpunit.result.cache
.phpunit.cache/
.DS_Store
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Time

This package aims to make working with times (as in date, ranges and/or duration) easier. It contains a time
value-object, ranges, duration, rounding, etc.
value-object, parser, ranges, duration, rounding, etc.

## Requirements

This package requires PHP 8.0 or higher.
This package requires PHP 8.1 or higher.

## Installation

Expand All @@ -26,6 +26,18 @@ The `Time` object can be initiated with:
$time = new Time(14, 30, 15);
```

#### Building from input

To build the `Time` object from several different inputs, you can use the following methods:

```php
TimeFactory::createFromString('14:30:15'); // Time object with 14 hours, 30 minutes and 15 seconds
TimeFactory::createFromDateTime(new \DateTime('2023-01-01 14:30:15')); // Time object with 14 hours, 30 minutes and 15 seconds
TimeFactory::createFromTimestamp(1640000000); // Time object with 11 hours, 33 minutes and 20 seconds
TimeFactory::createFromDurationInSeconds(9000); // Time object with 2 hours and 30 minutes
TimeFactory::createFromDurationInMinutes(150); // Time object with 2 hours and 30 minutes
```

#### Comparison

The `Time` object makes it easy to compare to other `Time` objects. It offers the following methods:
Expand Down Expand Up @@ -167,8 +179,8 @@ When you want a code coverage report which will be generated in the build/report

## Contribution

Any contribution is welcome, but it should meet the PSR-12 standard and please create one pull request per feature/bug.
In exchange, you will be credited as contributor on this page.
Any contribution is welcome, but it should meet the [PER 2.0 code style](https://www.php-fig.org/per/coding-style/) and
please create one pull request per feature/bug. In exchange, you will be credited as contributor.

## Security

Expand All @@ -177,9 +189,4 @@ instead of using the issue tracker.

## License

This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

## About vdhicts

[Vdhicts](https://www.vdhicts.nl) is the name of my personal company. Vdhicts develops and implements IT solutions for
businesses and educational institutions.
This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
25 changes: 18 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vdhicts/time",
"description": "Time value-object, collection and a range helper useful when working with times",
"description": "Time value-object with parser and collection and helpers for duration, rounding and ranges. Makes working with times easy.",
"homepage": "https://github.com/vdhicts/time",
"license": "MIT",
"authors": [
Expand All @@ -22,16 +22,27 @@
}
},
"require": {
"php": "^8.0"
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.0"
"phpstan/phpstan": "^1.11",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.2",
"symplify/easy-coding-standard": "^12.0"
},
"scripts": {
"test": "vendor/bin/phpunit --no-coverage",
"test-coverage": "vendor/bin/phpunit",
"analyse": "vendor/bin/phpstan analyse"
"test": [
"@analyze",
"@test:no-coverage",
"@lint:check"
],
"test:coverage": "vendor/bin/phpunit",
"test:no-coverage": "vendor/bin/phpunit --no-coverage",
"analyse": "vendor/bin/phpstan analyse",
"lint:check": "vendor/bin/ecs",
"lint:fix": "vendor/bin/ecs --fix",
"rector:check": "@rector:fix --dry-run",
"rector:fix": "@php vendor/bin/rector process"
},
"config": {
"sort-packages": true
Expand Down
16 changes: 16 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer;
use Symplify\EasyCodingStandard\Config\ECSConfig;

return ECSConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withSkip([
SingleLineEmptyBodyFixer::class,
])
->withPhpCsFixerSets(perCS20: true);
22 changes: 6 additions & 16 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
</report>
Expand All @@ -26,4 +11,9 @@
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
24 changes: 24 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->withPhpSets(php81: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
typeDeclarations: true,
privatization: true,
naming: true,
instanceOf: true,
earlyReturn: true,
strictBooleans: true,
carbon: true
)
->withImportNames();
6 changes: 3 additions & 3 deletions src/Exceptions/TimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class TimeException extends Exception
public static function unableToCreateFromString(string $value): self
{
return new self(
sprintf('Unable to create a Time value object from `%s`', $value)
sprintf('Unable to create a Time value object from `%s`', $value),
);
}

public static function invalidMinutes(int $minutes): self
{
return new self(
sprintf('Invalid minutes provided: `%d`, must be between 0 and 59', $minutes)
sprintf('Invalid minutes provided: `%d`, must be between 0 and 59', $minutes),
);
}

public static function invalidSeconds(int $seconds): self
{
return new self(
sprintf('Invalid seconds provided: `%d`, must be between 0 and 59', $seconds)
sprintf('Invalid seconds provided: `%d`, must be between 0 and 59', $seconds),
);
}

Expand Down
23 changes: 11 additions & 12 deletions src/TimeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

namespace Vdhicts\Time;

use Vdhicts\Time\Contracts;
use Vdhicts\Time\Contracts\TimeCollectionInterface;
use Vdhicts\Time\Contracts\TimeInterface;

class TimeCollection implements Contracts\TimeCollectionInterface
class TimeCollection implements TimeCollectionInterface
{
/**
* @var Contracts\TimeInterface[]
* @var TimeInterface[]
*/
private array $times = [];

/**
* @param Contracts\TimeInterface[] $times
* @param TimeInterface[] $times
*/
public function __construct(array $times = [])
{
$this->set($times);
}

public function add(Contracts\TimeInterface $time): Contracts\TimeCollectionInterface
public function add(TimeInterface $time): TimeCollectionInterface
{
$this->times[] = $time;

Expand All @@ -29,7 +30,7 @@ public function add(Contracts\TimeInterface $time): Contracts\TimeCollectionInte
/*
* @inheritDoc
*/
public function set(array $times): Contracts\TimeCollectionInterface
public function set(array $times): TimeCollectionInterface
{
$this->times = [];

Expand All @@ -40,15 +41,13 @@ public function set(array $times): Contracts\TimeCollectionInterface
return $this;
}

public function contains(Contracts\TimeInterface $time): bool
public function contains(TimeInterface $time): bool
{
$times = array_map(
function (Contracts\TimeInterface $time) {
return $time->toString();
},
$this->times
fn(TimeInterface $time): string => $time->toString(),
$this->times,
);

return in_array($time->toString(), $times);
return in_array($time->toString(), $times, true);
}
}
16 changes: 8 additions & 8 deletions src/TimeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TimeFactory
public static function createFromString(string $value): Time
{
$timestamp = strtotime($value);
if (! $timestamp) {
if ($timestamp === 0 || $timestamp === false) {
throw TimeException::unableToCreateFromString($value);
}

Expand All @@ -29,9 +29,9 @@ public static function createFromTimestamp(int $timestamp): Time
$seconds = date('s', $timestamp);

return (new Time())
->setHours((int)$hours)
->setMinutes((int)$minutes)
->setSeconds((int)$seconds);
->setHours((int) $hours)
->setMinutes((int) $minutes)
->setSeconds((int) $seconds);
}

/**
Expand All @@ -41,9 +41,9 @@ public static function createFromDurationInSeconds(int $seconds): Time
{
$hours = floor($seconds / 3600);
$minutes = floor($seconds / 60) % 60;
$seconds = $seconds % 60;
$seconds %= 60;

return new Time((int)$hours, $minutes, $seconds);
return new Time((int) $hours, $minutes, $seconds);
}

/**
Expand All @@ -52,8 +52,8 @@ public static function createFromDurationInSeconds(int $seconds): Time
public static function createFromDurationInMinutes(int $minutes): Time
{
$hours = floor($minutes / 60);
$minutes = $minutes % 60;
$minutes %= 60;

return new Time((int)$hours, $minutes, 0);
return new Time((int) $hours, $minutes, 0);
}
}
26 changes: 12 additions & 14 deletions src/TimeRange.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,31 @@

namespace Vdhicts\Time;

use Vdhicts\Time\Contracts;
use Vdhicts\Time\Contracts\TimeInterface;
use Vdhicts\Time\Contracts\TimeRangeInterface;

class TimeRange implements Contracts\TimeRangeInterface
class TimeRange implements TimeRangeInterface
{
private Contracts\TimeInterface $start;
private Contracts\TimeInterface $end;

public function __construct(Contracts\TimeInterface $start, Contracts\TimeInterface $end)
{
$this->start = $start;
$this->end = $end;
public function __construct(
private readonly TimeInterface $start,
private readonly TimeInterface $end,
) {
}

public function getStart(): Contracts\TimeInterface
public function getStart(): TimeInterface
{
return $this->start;
}

public function getEnd(): Contracts\TimeInterface
public function getEnd(): TimeInterface
{
return $this->end;
}

/**
* Determines if the time is in between or equals to the start and end of this time range.
*/
public function inRange(Contracts\TimeInterface $time): bool
public function inRange(TimeInterface $time): bool
{
$startIsAfterOrEqualTo = $time->isAfterOrEqualTo($this->start);
$endIsBeforeOrEqualTo = $time->isBeforeOrEqualTo($this->end);
Expand All @@ -39,7 +37,7 @@ public function inRange(Contracts\TimeInterface $time): bool
/**
* Determine if the time ranges are overlapping each other.
*/
public function isOverlapping(Contracts\TimeRangeInterface $timeRange): bool
public function isOverlapping(TimeRangeInterface $timeRange): bool
{
$startIsBeforeOrEqualToEnd = $this
->getStart()
Expand All @@ -54,7 +52,7 @@ public function isOverlapping(Contracts\TimeRangeInterface $timeRange): bool
/**
* Returns the time object for the duration of the range.
*/
public function getRangeDuration(): Contracts\TimeInterface
public function getRangeDuration(): TimeInterface
{
return $this
->start
Expand Down
Loading
Loading