Skip to content

Commit

Permalink
Add psalm and phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Jun 3, 2020
1 parent aaa9dbf commit 62c524a
Show file tree
Hide file tree
Showing 59 changed files with 602 additions and 649 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.DS_Store
/.php_cs.cache
/.phpunit.result.cache
/composer.lock
/vendor/
composer.lock
25 changes: 25 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests');

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'@Symfony' => true,
'array_syntax' => ['syntax' => 'short'],
'blank_line_before_statement' => false,
'concat_space' => ['spacing' => 'one'],
'function_declaration' => ['closure_function_spacing' => 'none'],
'increment_style' => false,
'phpdoc_align' => ['align' => 'left'],
'phpdoc_separation' => false,
'phpdoc_summary' => false,
'phpdoc_to_comment' => false,
'yoda_style' => false,
])
->setFinder($finder);

// __END__
// vim: filetype=php
12 changes: 6 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

cache:
directories:
Expand All @@ -18,9 +16,11 @@ install:

script:
- mkdir -p build/logs
- php ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- ./vendor/bin/psalm --no-progress
- ./vendor/bin/php-cs-fixer fix -v --dry-run --stop-on-violation --using-cache=no
- ./vendor/bin/phpunit --verbose --coverage-clover build/logs/clover.xml

after_script:
- travis_retry php ./vendor/bin/coveralls
- travis_retry php ./vendor/bin/php-coveralls

sudo: false
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^4.4",
"satooshi/php-coveralls": "^1.0"
"phpunit/phpunit": "^7.0",
"php-coveralls/php-coveralls": "^2.0",
"friendsofphp/php-cs-fixer": "^2.16",
"vimeo/psalm": "~3.11.0"
},
"autoload": {
"psr-4": {
Expand Down
21 changes: 7 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true">
<phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true">
<php>
<ini name="memory_limit" value="128M" />
<ini name="error_reporting" value="-1" />
<ini name="memory_limit" value="-1" />
<ini name="zend.assertions " value="1" />
</php>

<testsuites>
<testsuite>
<directory>./tests/</directory>
<testsuite name="tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./src/</directory>
</whitelist>
</filter>
Expand Down
13 changes: 13 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<psalm
totallyTyped="true"
errorLevel="2"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
</projectFiles>
</psalm>
34 changes: 13 additions & 21 deletions src/Distribution/AbstractDistribution.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
<?php

declare(strict_types=1);

namespace Emonkak\Random\Distribution;

use Emonkak\Random\Engine\AbstractEngine;
use Emonkak\Random\Engine\EngineInterface;

abstract class AbstractDistribution
/**
* @template T
* @implements DistributionInterface<T>
*/
abstract class AbstractDistribution implements DistributionInterface
{
/**
* @param AbstractEngine $engine
* @return mixed
*/
public function __invoke(AbstractEngine $engine)
{
return $this->generate($engine);
}

/**
* @param AbstractEngine $engine
* @return mixed
*/
abstract public function generate(AbstractEngine $engine);

/**
* @param AbstractEngine $engine
* @return \Iterator
* {@inheritdoc}
*/
public function iterate(AbstractEngine $engine)
public function iterate(EngineInterface $engine): \Iterator
{
return new DistributionIterator($this, $engine);
while (true) {
yield $this->generate($engine);
}
}
}
19 changes: 9 additions & 10 deletions src/Distribution/BernoulliDistribution.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
<?php

declare(strict_types=1);

namespace Emonkak\Random\Distribution;

use Emonkak\Random\Engine\AbstractEngine;
use Emonkak\Random\Engine\EngineInterface;

/**
* @extends AbstractDistribution<bool>
*/
class BernoulliDistribution extends AbstractDistribution
{
/**
* @var float
*/
private $p;

/**
* @param float $p
*/
public function __construct($p)
public function __construct(float $p)
{
assert(0 <= $p && $p <= 1);

$this->p = $p;
}

/**
* @return float
*/
public function getP()
public function getP(): float
{
return $this->p;
}

/**
* {@inheritdoc}
*/
public function generate(AbstractEngine $engine)
public function generate(EngineInterface $engine)
{
if ($this->p == 0) {
return false;
Expand Down
31 changes: 13 additions & 18 deletions src/Distribution/BinomialDistribution.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<?php

declare(strict_types=1);

namespace Emonkak\Random\Distribution;

use Emonkak\Random\Engine\AbstractEngine;
use Emonkak\Random\Engine\EngineInterface;

/**
* @extends AbstractDistribution<int>
*/
class BinomialDistribution extends AbstractDistribution
{
/**
* @var integer
* @var int
*/
private $n;
private $t;

/**
* @var float
*/
private $probability;
private $p;

/**
* @param integer $t
* @param float $p
*/
public function __construct($t, $p)
public function __construct(int $t, float $p)
{
assert(0 <= $t);
assert(0 <= $p && $p <= 1);
Expand All @@ -29,26 +30,20 @@ public function __construct($t, $p)
$this->p = $p;
}

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

/**
* @return float
*/
public function getP()
public function getP(): float
{
return $this->p;
}

/**
* {@inheritdoc}
*/
public function generate(AbstractEngine $engine)
public function generate(EngineInterface $engine)
{
if ($this->p == 0) {
return 0;
Expand Down
24 changes: 13 additions & 11 deletions src/Distribution/DiscreteDistribution.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<?php

declare(strict_types=1);

namespace Emonkak\Random\Distribution;

use Emonkak\Random\Engine\AbstractEngine;
use Emonkak\Random\Engine\EngineInterface;

/**
* @extends AbstractDistribution<int>
*/
class DiscreteDistribution extends AbstractDistribution
{
/**
Expand All @@ -17,47 +22,44 @@ class DiscreteDistribution extends AbstractDistribution
private $uniform;

/**
* @param iterable $probabilities
* @param float[] $probabilities
*/
public function __construct($probabilities)
public function __construct(array $probabilities)
{
assert(!empty($probabilities));

$totalProbability = 0;

foreach ($probabilities as $probability) {
$totalProbability += $probability;
}

// TODO: Use alias table
$this->probabilities = $probabilities;
$this->uniform = new UniformRealDistribution(0, $totalProbability);
}

/**
* @return float[]
*/
public function getProbabilities()
public function getProbabilities(): array
{
return $this->probabilities;
}

/**
* {@inheritdoc}
*/
public function generate(AbstractEngine $engine)
public function generate(EngineInterface $engine)
{
$result = $this->uniform->generate($engine);
$sum = 0;

foreach ($this->probabilities as $key => $probability) {
foreach ($this->probabilities as $index => $probability) {
$sum += $probability;

if ($probability > 0 && $result <= $sum) {
return $key;
return $index;
}
}

throw new \LogicException("Can't generate the random number. Confirm the probabilities.");
throw new \RuntimeException('Cannot generate the random number. Please confirm the probabilities.');
}
}
23 changes: 23 additions & 0 deletions src/Distribution/DistributionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Emonkak\Random\Distribution;

use Emonkak\Random\Engine\EngineInterface;

/**
* @template T
*/
interface DistributionInterface
{
/**
* @return T
*/
public function generate(EngineInterface $engine);

/**
* @return \Iterator<T>
*/
public function iterate(EngineInterface $engine): \Iterator;
}

0 comments on commit 62c524a

Please sign in to comment.