Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  fix action argument
  use actions/checkout 3
  use ramsey/composer-install
  add shortcut to convert a period to an elapsed period
  require php 8.1
  • Loading branch information
Baptouuuu committed Feb 11, 2023
2 parents 88e099d + 486baed commit a7110e5
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 53 deletions.
59 changes: 14 additions & 45 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,22 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest]
php-version: ['8.0', '8.1']
php-version: ['8.1', '8.2']
dependencies: ['lowest', 'highest']
name: 'PHPUnit'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
coverage: xdebug
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
- name: Composer
uses: "ramsey/composer-install@v2"
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-progress
- name: Install Dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer install --no-progress
dependency-versions: ${{ matrix.dependencies }}
- name: PHPUnit
run: vendor/bin/phpunit --coverage-clover=coverage.clover
- uses: codecov/codecov-action@v1
Expand All @@ -44,58 +33,38 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.0', '8.1']
php-version: ['8.1', '8.2']
dependencies: ['lowest', 'highest']
name: 'Psalm'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
- name: Composer
uses: "ramsey/composer-install@v2"
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install Dependencies
if: ${{ matrix.dependencies == 'lowest' }}
run: composer update --prefer-lowest --no-progress
- name: Install Dependencies
if: ${{ matrix.dependencies == 'highest' }}
run: composer install --no-progress
dependency-versions: ${{ matrix.dependencies }}
- name: Psalm
run: vendor/bin/psalm --shepherd
cs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.0']
php-version: ['8.1']
name: 'CS'
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl
- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.php-version }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-${{ matrix.php-version }}-composer-
- name: Install Dependencies
run: composer install --no-progress
- name: Composer
uses: "ramsey/composer-install@v2"
- name: CS
run: vendor/bin/php-cs-fixer fix --diff --dry-run
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 3.2.0 - 2023-02-11

### Added

- `Innmind\TimeContinuum\Earth\Period`
- `Innmind\TimeContinuum\Earth\ElapsedPeriod::ofPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Millisecond::asElapsedPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Second::asElapsedPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Minute::asElapsedPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Hour::asElapsedPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Day::asElapsedPeriod()`
- `Innmind\TimeContinuum\Earth\Period\Composite::asElapsedPeriod()`

### Changed

- Require PHP `8.1`

## 3.1.0 - 2022-11-14

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"issues": "http://github.com/Innmind/TimeContinuum/issues"
},
"require": {
"php": "~8.0",
"php": "~8.1",
"psr/log": "~3.0",
"innmind/immutable": "~4.0"
},
Expand Down
25 changes: 24 additions & 1 deletion src/Earth/ElapsedPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use Innmind\TimeContinuum\{
ElapsedPeriod as ElapsedPeriodInterface,
Exception\ElapsedPeriodCantBeNegative
Period as PeriodInterface,
Exception\ElapsedPeriodCantBeNegative,
Exception\LogicException,
};
use Innmind\Immutable\Maybe;

Expand Down Expand Up @@ -50,6 +52,27 @@ public static function maybe(int $milliseconds): Maybe
}
}

/**
* @psalm-pure
*
* @throws LogicException When using a period containing months or years
*/
public static function ofPeriod(PeriodInterface $period): self
{
if ($period->months() !== 0 || $period->years() !== 0) {
// a month or a year is not constant
throw new LogicException('Months and years can not be converted to milliseconds');
}

$milliseconds = Period::day->milliseconds($period->days()) +
Period::hour->milliseconds($period->hours()) +
Period::minute->milliseconds($period->minutes()) +
Period::second->milliseconds($period->seconds()) +
$period->milliseconds();

return new self($milliseconds);
}

public function milliseconds(): int
{
return $this->milliseconds;
Expand Down
29 changes: 29 additions & 0 deletions src/Earth/Period.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types = 1);

namespace Innmind\TimeContinuum\Earth;

/**
* @psalm-immutable
*/
enum Period
{
case second;
case minute;
case hour;
case day;

/**
* Returns the number of milliseconds contained in the number of seconds,
* minutes, hours and days
*/
public function milliseconds(int $number): int
{
return match ($this) {
self::second => $number * 1000,
self::minute => $number * self::second->milliseconds(60),
self::hour => $number * self::minute->milliseconds(60),
self::day => $number * self::hour->milliseconds(24),
};
}
}
12 changes: 10 additions & 2 deletions src/Earth/Period/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace Innmind\TimeContinuum\Earth\Period;

use Innmind\TimeContinuum\Period;
use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
};

/**
* @psalm-immutable
Expand Down Expand Up @@ -101,7 +104,7 @@ public function equals(Period $period): bool
$this->millisecond === $period->milliseconds();
}

public function add(Period $period): Period
public function add(Period $period): self
{
return new self(
$this->year + $period->years(),
Expand All @@ -113,4 +116,9 @@ public function add(Period $period): Period
$this->millisecond + $period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
8 changes: 7 additions & 1 deletion src/Earth/Period/Day.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
Exception\PeriodCantBeNegative,
};

Expand Down Expand Up @@ -70,7 +71,7 @@ public function equals(Period $period): bool
$period->milliseconds() === 0;
}

public function add(Period $period): Period
public function add(Period $period): Composite
{
return new Composite(
$period->years(),
Expand All @@ -82,4 +83,9 @@ public function add(Period $period): Period
$period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
8 changes: 7 additions & 1 deletion src/Earth/Period/Hour.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
Exception\PeriodCantBeNegative,
};

Expand Down Expand Up @@ -72,7 +73,7 @@ public function equals(Period $period): bool
$period->milliseconds() === 0;
}

public function add(Period $period): Period
public function add(Period $period): Composite
{
return new Composite(
$period->years(),
Expand All @@ -84,4 +85,9 @@ public function add(Period $period): Period
$period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
8 changes: 7 additions & 1 deletion src/Earth/Period/Millisecond.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
Exception\PeriodCantBeNegative,
};

Expand Down Expand Up @@ -72,7 +73,7 @@ public function equals(Period $period): bool
$period->milliseconds() === $this->millisecond;
}

public function add(Period $period): Period
public function add(Period $period): Composite
{
return new Composite(
$period->years(),
Expand All @@ -84,4 +85,9 @@ public function add(Period $period): Period
$this->millisecond + $period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
8 changes: 7 additions & 1 deletion src/Earth/Period/Minute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
Exception\PeriodCantBeNegative,
};

Expand Down Expand Up @@ -72,7 +73,7 @@ public function equals(Period $period): bool
$period->milliseconds() === 0;
}

public function add(Period $period): Period
public function add(Period $period): Composite
{
return new Composite(
$period->years(),
Expand All @@ -84,4 +85,9 @@ public function add(Period $period): Period
$period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
6 changes: 6 additions & 0 deletions src/Earth/Period/Second.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Innmind\TimeContinuum\{
Period,
Earth\ElapsedPeriod,
Exception\PeriodCantBeNegative,
};

Expand Down Expand Up @@ -84,4 +85,9 @@ public function add(Period $period): Period
$period->milliseconds(),
);
}

public function asElapsedPeriod(): ElapsedPeriod
{
return ElapsedPeriod::ofPeriod($this);
}
}
17 changes: 17 additions & 0 deletions tests/Earth/ElapsedPeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

use Innmind\TimeContinuum\{
Earth\ElapsedPeriod,
Earth\Period\Year,
Earth\Period\Month,
Exception\ElapsedPeriodCantBeNegative,
Exception\LogicException,
};
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -75,4 +78,18 @@ public function testEquals()
),
);
}

public function testThrowWhenTryingToBuildFromYearPeriod()
{
$this->expectException(LogicException::class);

ElapsedPeriod::ofPeriod(new Year(1));
}

public function testThrowWhenTryingToBuildFromMonthPeriod()
{
$this->expectException(LogicException::class);

ElapsedPeriod::ofPeriod(new Month(1));
}
}
Loading

0 comments on commit a7110e5

Please sign in to comment.