Skip to content

Commit 9306efd

Browse files
authored
Merge pull request #5 from skywarth/php-version-upgrade
Upgrade to PHP 8.1 and using PHPStan
2 parents 82e1968 + 7aca8b8 commit 9306efd

File tree

12 files changed

+71
-29
lines changed

12 files changed

+71
-29
lines changed

.github/workflows/laravel.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ jobs:
2626
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
2727
- name: Install Dependencies
2828
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
29+
- name: Run Static Analysis
30+
run: vendor/bin/phpstan analyse --memory-limit=512M
2931
- name: Execute tests (Unit and Feature tests) via PHPUnit
3032
run: vendor/bin/phpunit
3133
- name: Upload coverage reports to Codecov

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
],
2323
"require-dev": {
2424
"orchestra/testbench": "^6.28",
25-
"phpunit/phpunit": "^9.6"
25+
"phpunit/phpunit": "^9.6",
26+
"phpstan/phpstan": "^2.1"
2627
},
2728
"extra": {
2829
"laravel": {
@@ -36,12 +37,13 @@
3637
"scripts": {
3738
"test":"./vendor/bin/phpunit",
3839
"test-random-time-macros":"@test --filter RandomTimeMacros",
39-
"test-random-date-macros":"@test --filter RandomDateMacrosTest"
40+
"test-random-date-macros":"@test --filter RandomDateMacrosTest",
41+
"static-analysis": "vendor/bin/phpstan analyse"
4042
},
4143
"minimum-stability": "stable",
4244
"prefer-stable": true,
4345
"require": {
44-
"php": ">=7.4",
46+
"php": ">=8.1",
4547
"paragonie/seedspring": "^1.2"
4648
}
4749
}

phpstan.neon

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
- config
6+
7+
excludePaths:
8+
- src/RNGs/MersenneTwister.php

src/Enums/RandomDateScheduleBasis.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class RandomDateScheduleBasis
88
{
9+
//TODO: Change to PHP 8.X Enum
910
public const WEEK=10;
1011
public const MONTH=20;
1112

@@ -32,6 +33,9 @@ public static function isValid(int $basis):bool{
3233
return in_array($basis,self::getAll());
3334
}
3435

36+
/**
37+
* @return int[]
38+
*/
3539
public static function getAll():array{
3640
return[
3741
'WEEK'=>self::WEEK,

src/Exceptions/IncorrectRangeException.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,27 @@
55
class IncorrectRangeException extends \Exception
66
{
77

8-
private string $min;
9-
private string $max;
10-
118
/**
129
* @param string $min
1310
* @param string $max
1411
*/
15-
public function __construct(string $min, string $max)
12+
public function __construct(private string $min, private string $max)
1613
{
17-
$this->min = $min;
18-
$this->max = $max;
19-
$msg="${min} is bigger/later than ${max}! Please correct your parameters.";
14+
$msg="{$this->getMin()} is bigger/later than {$this->getMax()}! Please correct your parameters.";
2015
parent::__construct($msg);
2116
}
2217

18+
public function getMax(): string
19+
{
20+
return $this->max;
21+
}
22+
23+
public function getMin(): string
24+
{
25+
return $this->min;
26+
}
27+
28+
29+
2330

2431
}

src/Exceptions/MissingSeedException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace Skywarth\ChaoticSchedule\Exceptions;
44

5+
use Throwable;
6+
57
class MissingSeedException extends \Exception
68
{
7-
protected $message='RNGAdapter is missing seed value! Set the seed first, before accessing';
9+
public function __construct(string $message = 'RNGAdapter is missing seed value! Set the seed first, before accessing', int $code = 0, ?Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
813

914
}

src/RNGs/Adapters/AbstractRNGAdapter.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ abstract class AbstractRNGAdapter implements RandomNumberGeneratorAdapter
1010

1111
protected int $seed;
1212

13-
public function __construct(int $seed=null)
13+
/**
14+
* @throws InvalidSeedFormatException
15+
*/
16+
public function __construct(?int $seed=null)
1417
{
1518
if(!is_null($seed)){
1619
$this->setSeed($seed);
@@ -31,7 +34,9 @@ public function getSeed(): int
3134
}
3235

3336

34-
37+
/**
38+
* @throws InvalidSeedFormatException
39+
*/
3540
public final function setSeed(int $seed):RandomNumberGeneratorAdapter
3641
{
3742
if(!$this->validateSeed($seed)){//Maybe another method for padding the missing bytes/length ?

src/RNGs/Adapters/RandomNumberGeneratorAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
interface RandomNumberGeneratorAdapter
88
{
9-
public function __construct(int $seed=null);//TODO: maybe make seed into string ?
9+
public function __construct(?int $seed=null);//TODO: maybe make seed into string ?
1010
1111
public function setSeed(int $seed):RandomNumberGeneratorAdapter;
1212
public function getSeed():int;

src/RNGs/Adapters/SeedSpringAdapter.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Skywarth\ChaoticSchedule\RNGs\Adapters;
44

55

6+
use Exception;
67
use ParagonIE\SeedSpring\SeedSpring;
78

89
class SeedSpringAdapter extends AbstractRNGAdapter
@@ -12,6 +13,9 @@ class SeedSpringAdapter extends AbstractRNGAdapter
1213
private SeedSpring $seedSpring;
1314

1415

16+
/**
17+
* @throws Exception
18+
*/
1519
public function intBetween(int $floor, int $ceil): int
1620
{
1721
//Boundaries are inclusive
@@ -32,12 +36,12 @@ public function validateSeed(int $seed): bool
3236
{
3337

3438
// Check the length of the binary representation
35-
return strlen($seed) === (self::PROVIDER_SEED_BYTES);
39+
return strlen((string)$seed) === (self::PROVIDER_SEED_BYTES);
3640
}
3741

3842
protected function setProviderSeed(int $seed): RandomNumberGeneratorAdapter
3943
{
40-
$this->seedSpring=new SeedSpring($seed);
44+
$this->seedSpring=new SeedSpring((string)$seed);
4145
return $this;
4246
}
4347
}

src/RNGs/RNGFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class RNGFactory
1717
public function __construct(string $rngEngineSlug)
1818
{
1919
$this->rngEngineSlug = $rngEngineSlug;
20-
return $this;
2120
}
2221

2322
/**
@@ -29,7 +28,7 @@ public function getRngEngineSlug(): string
2928
}
3029

3130

32-
public function getRngEngine(int $seed=null):RandomNumberGeneratorAdapter{
31+
public function getRngEngine(?int $seed=null):RandomNumberGeneratorAdapter{
3332
//TODO: array containing ::class for comparison, you don't really need if-else
3433
if($this->getRngEngineSlug()===MersenneTwisterAdapter::getAdapterSlug()){
3534
return new MersenneTwisterAdapter($seed);

0 commit comments

Comments
 (0)