Skip to content

Commit

Permalink
Add PHPStan level 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Smirnov committed Jun 26, 2024
1 parent def9a32 commit e8a4648
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ jobs:
runs-on: ubuntu-22.04

strategy:
fail-fast: false
experimental: [false]
matrix:
php-version:
- 7.3
Expand All @@ -17,6 +19,9 @@ jobs:
- 8.1
- 8.2
- 8.3
include:
- php: 8.2
analysis: true


steps:
Expand All @@ -35,5 +40,9 @@ jobs:
with:
composer-options: --prefer-dist

- name: Static analysis
if: matrix.analysis
run: vendor/bin/phpstan

- name: Run Tests
run: vendor/bin/phpunit
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "kohanaworld/hmmmath",
"description": "Collection of math related PHP functions (PHP >=7.2 <=8.3",
"license": "MIT",
"version": "0.9.0",
"authors": [
{
"name": "Lars Strojny",
Expand All @@ -24,9 +23,11 @@
}
},
"require": {
"php": "^7.3|^8.0"
"php": "^7.4|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^9",
"phpstan/phpstan": "^1.11",
"squizlabs/php_codesniffer": "3.*"
}
}
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 2
paths:
- src
- tests
2 changes: 1 addition & 1 deletion src/hmmmath/Exception/BadMethodCallException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use InvalidArgumentException as BaseBadMethodCallException;

class BadMethodCallException extends BaseBadMethodCallException
final class BadMethodCallException extends BaseBadMethodCallException
{
public static function invalidMethod(string $className, string $methodName): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/hmmmath/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use InvalidArgumentException as BaseInvalidArgumentException;

class InvalidArgumentException extends BaseInvalidArgumentException
final class InvalidArgumentException extends BaseInvalidArgumentException
{
/**
* @param mixed $actualValue
Expand Down
20 changes: 13 additions & 7 deletions src/hmmmath/Fibonacci/FibonacciFactory.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<?php
namespace hmmmath\Fibonacci;

use ArrayIterator;
use hmmmath\Exception\InvalidArgumentException;
use Traversable;
use LimitIterator;

class FibonacciFactory
{
/**
* Create fibonacci sequence
*
* The sequence is infinite but the factory method optionally takes a third argument to limit the sequence. A fourth
* parameter can be passed to specify an offset for the sequence.
*/
/**
* Create fibonacci sequence
*
* The sequence is infinite but the factory method optionally takes a third argument to limit the sequence. A fourth
* parameter can be passed to specify an offset for the sequence.
* @param int $start
* @param int $increment
* @param int $limit
* @param int $offset
* @return Traversable
*/
public static function sequence(int $start = 0, int $increment = 1, int $limit = 0, int $offset = 0): Traversable
{
$sequence = new FibonacciSequence($start, $increment);
Expand All @@ -26,7 +32,7 @@ public static function sequence(int $start = 0, int $increment = 1, int $limit =

return $sequence;
}

//@return ArrayIterator<int, FibonacciSequence>|LimitIterator<mixed, mixed,FibonacciSequence>
/** Create fibonacci number */
public static function number(int $start = 0, int $increment = 1): FibonacciNumber
{
Expand Down
4 changes: 2 additions & 2 deletions src/hmmmath/Fibonacci/FibonacciNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

class FibonacciNumber
{
private $start;
private $increment;
private int $start;
private int $increment;

public function __construct(int $start, int $increment)
{
Expand Down
40 changes: 30 additions & 10 deletions src/hmmmath/Fibonacci/FibonacciSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,70 @@
use Iterator;
use hmmmath\Exception\BadMethodCallException;


/**
* @implements Iterator<int, FibonacciNumber>
* @implements ArrayAccess<int, FibonacciNumber>
*/
class FibonacciSequence implements Iterator, ArrayAccess
{
/** @var FibonacciNumber */
private $number;
private FibonacciNumber $number;

/** @var FibonacciNumber */
private $initial;
private FibonacciNumber $initial;

/** @var int */
private $key = 0;
private int $key = 0;

public function __construct(int $start = 0, int $increment = 1)
{
$this->number = $this->initial = new FibonacciNumber($start, $increment);
}

public function current(): int
/**
* @return int
*/
public function current(): int
{
return $this->number->getCurrent();
}

public function key(): int
/**
* @return int
*/
public function key(): int
{
return $this->key;
}

public function valid(): bool
/**
* @return bool
*/
public function valid(): bool
{
return true;
}

public function rewind(): void
/**
* @return void
*/
public function rewind(): void
{
$this->key = 0;
$this->number = $this->initial;
}

public function next(): void
/**
* @return void
*/
public function next(): void
{
++$this->key;
$this->number = $this->number->getNext();
}

public function offsetExists($offset): bool // @codingStandardsIgnoreLine
public function offsetExists($offset): bool // @codingStandardsIgnoreLine
{
return true;
}
Expand All @@ -64,7 +84,7 @@ public function offsetGet($offset): int // @codingStandardsIgnoreLine
return $number->getCurrent();
}

public function offsetSet($offset, $value): void // @codingStandardsIgnoreLine
public function offsetSet($offset, $value): void // @codingStandardsIgnoreLine
{
throw BadMethodCallException::invalidMethod(__CLASS__, __FUNCTION__);
}
Expand Down
2 changes: 1 addition & 1 deletion src/hmmmath/Percentile/Percentile.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static function percentile(
string $mode = self::NEAREST_RANK,
bool $sort = true
): float {
InvalidArgumentException::assertParameterType('2', 'double', $percentile, '0..1');
InvalidArgumentException::assertParameterType(2, 'double', $percentile, '0..1');

if ($sort) {
sort($numbers);
Expand Down

0 comments on commit e8a4648

Please sign in to comment.