Skip to content

Commit

Permalink
Installed psalm, replaced cs fixer, PER (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavche authored Mar 4, 2024
1 parent 02b8c3d commit 3072607
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
vendor/
.idea
.php_cs.cache
.php-cs-fixer.cache
infection.log
composer.lock
build
Expand Down
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__)
;

return (new PhpCsFixer\Config())
->setRules([
'@PER-CS2.0' => true,
'@PHP82Migration' => true,
])
->setFinder($finder)
;
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ $ ./vendor/bin/phpstan analyse src/ -c phpstan.neon --level=9 --no-progress -vvv
##### PHP-CS
###### Code style check
```bash
$ composer phpcs
$ ./vendor/bin/phpcs
$ composer phpcs-check
$ ./vendor/bin/php-cs-fixer check --diff

```
###### Code style fix
```bash
$ composer phpcbf
$ ./vendor/bin/phpcbf
$ composer phpcs-fix
$ ./vendor/bin/php-cs-fixer fix
```

## License
Expand Down
1 change: 1 addition & 0 deletions benchmarks/GetSetBitsIndexBench.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Yaroslavche\Benchmarks;
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/IndexToBitBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function benchIndexToBit2(array $index): void

private function indexToBit1(int $index): int
{
return (int)pow(2, $index);
return (int) pow(2, $index);
}

private function indexToBit2(int $index): int
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
"phpunit/phpunit": "*",
"phpbench/phpbench": "*",
"phpstan/phpstan": "*",
"squizlabs/php_codesniffer": "*",
"thecodingmachine/phpstan-strict-rules": "*",
"infection/infection": "*"
"infection/infection": "*",
"vimeo/psalm": "^5.22",
"psalm/plugin-phpunit": "^0.18.4",
"friendsofphp/php-cs-fixer": "^3.51"
},
"authors": [
{
Expand All @@ -38,15 +40,16 @@
}
},
"scripts": {
"phpcs": "phpcs",
"phpcbf": "phpcbf",
"phpcs-check": "php-cs-fixer check --diff",
"phpcs-fix": "php-cs-fixer fix --diff",
"phpstan": "phpstan analyse src/ -c phpstan.neon --level=9 --no-progress -vv --memory-limit=-1",
"psalm": "psalm",
"phpbench": "phpbench run benchmarks --report=default",
"phpunit": "phpunit",
"infection": "XDEBUG_MODE=coverage infection --min-msi=100 --min-covered-msi=100 --log-verbosity=all",
"coverage": "XDEBUG_MODE=coverage phpunit --coverage-text --coverage-html ./build/coverage/html --coverage-clover ./build/coverage/clover.xml",
"ci:pack": [
"@phpcs", "@phpstan", "@phpunit", "@infection", "@coverage"
"@phpcs-check", "@phpstan", "@psalm", "@phpunit", "@infection", "@coverage"
]
},
"minimum-stability": "stable",
Expand Down
15 changes: 0 additions & 15 deletions phpcs.xml.dist

This file was deleted.

20 changes: 20 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
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"
findUnusedBaselineEntry="true"
findUnusedCode="true"
>
<projectFiles>
<directory name="src"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
11 changes: 7 additions & 4 deletions src/BitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(

public function __toString(): string
{
return (string)$this->mask;
return (string) $this->mask;
}

public function get(): int
Expand Down Expand Up @@ -52,8 +52,11 @@ public function has(int ...$bits): bool
/** @throws OutOfRangeException */
private function checkMask(int $mask): void
{
if ($mask < 0 || $this->mostSignificantBit && $mask >= Bits::indexToBit($this->mostSignificantBit + 1)) {
throw new OutOfRangeException((string)$mask);
if (
$mask < 0 ||
null !== $this->mostSignificantBit && $mask >= Bits::indexToBit($this->mostSignificantBit + 1)
) {
throw new OutOfRangeException((string) $mask);
}
}

Expand All @@ -65,7 +68,7 @@ private function checkBit(int $bit): void
{
$this->checkMask($bit);
if (!Bits::isSingleBit($bit)) {
throw new NotSingleBitException((string)$bit);
throw new NotSingleBitException((string) $bit);
}
}
}
2 changes: 2 additions & 0 deletions src/EnumBitMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use BitMask\Util\Bits;
use UnitEnum;

/** @psalm-suppress UnusedClass */
final class EnumBitMask implements BitMaskInterface
{
private BitMask $bitmask;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function has(UnitEnum ...$bits): bool
$bit instanceof $this->enum ?:
throw new UnknownEnumException(sprintf('Expected %s enum, %s provided', $this->enum, $bit::class))
);
/** @psalm-var UnitEnum[] $bits */
return $this->bitmask->has(...$this->enumToInt(...$bits));
}

Expand Down
4 changes: 1 addition & 3 deletions src/Exception/BitMaskExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Throwable;

interface BitMaskExceptionInterface extends Throwable
{
}
interface BitMaskExceptionInterface extends Throwable {}
4 changes: 1 addition & 3 deletions src/Exception/NotSingleBitException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use Exception;

final class NotSingleBitException extends Exception implements BitMaskExceptionInterface
{
}
final class NotSingleBitException extends Exception implements BitMaskExceptionInterface {}
4 changes: 1 addition & 3 deletions src/Exception/OutOfRangeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use OutOfRangeException as SplOutOfRangeException;

final class OutOfRangeException extends SplOutOfRangeException implements BitMaskExceptionInterface
{
}
final class OutOfRangeException extends SplOutOfRangeException implements BitMaskExceptionInterface {}
4 changes: 1 addition & 3 deletions src/Exception/UnknownEnumException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@

use Exception;

final class UnknownEnumException extends Exception implements BitMaskExceptionInterface
{
}
final class UnknownEnumException extends Exception implements BitMaskExceptionInterface {}
9 changes: 7 additions & 2 deletions src/Util/Bits.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class Bits
*/
public static function getMostSignificantBit(int $mask): int
{
return (int)log($mask, 2);
return (int) log($mask, 2);
}

/**
Expand Down Expand Up @@ -50,6 +50,7 @@ public static function isSingleBit(int $mask): bool
/**
* single bit to index (left > right)
* @throws NotSingleBitException
* @psalm-suppress PossiblyUnusedMethod
*/
public static function bitToIndex(int $mask): int
{
Expand All @@ -70,11 +71,12 @@ public static function bitToIndex(int $mask): int
public static function indexToBit(int $index): int
{
if ($index < 0) {
throw new OutOfRangeException((string)$index);
throw new OutOfRangeException((string) $index);
}
return intval(abs(1 << $index));
}

/** @psalm-suppress PossiblyUnusedMethod */
public static function toString(int $mask): string
{
return decbin($mask);
Expand All @@ -84,6 +86,7 @@ public static function toString(int $mask): string
* @return int[]
* @see benchmarks/GetSetBitsIndexBench.php
* ./vendor/bin/phpbench run benchmarks/GetSetBitsIndexBench.php --report=default
* @psalm-suppress PossiblyUnusedMethod
*/
public static function getSetBitsIndexes(int $mask): array
{
Expand All @@ -98,6 +101,7 @@ public static function getSetBitsIndexes(int $mask): array
* Bitwise-based check if given number is even
* @see benchmarks/EvenOddBench.php
* ./vendor/bin/phpbench run benchmarks/EvenOddBench.php --report=default
* @psalm-suppress PossiblyUnusedMethod
*/
public static function isEvenNumber(int $number): bool
{
Expand All @@ -108,6 +112,7 @@ public static function isEvenNumber(int $number): bool
* Bitwise-based check if given number is odd
* @see benchmarks/EvenOddBench.php
* ./vendor/bin/phpbench run benchmarks/EvenOddBench.php --report=default
* @psalm-suppress PossiblyUnusedMethod
*/
public static function isOddNumber(int $number): bool
{
Expand Down
18 changes: 9 additions & 9 deletions tests/BitMaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testSet(): void
$bitmask->set(self::READ);
assertEquals(self::READ, $bitmask->get());
$this->expectException(OutOfRangeException::class);
$this->expectExceptionMessage((string)self::EXECUTE);
$this->expectExceptionMessage((string) self::EXECUTE);
$bitmask->set(self::EXECUTE);
}

Expand All @@ -65,15 +65,15 @@ public function testHasNotSingleBit(): void
{
$bitmask = new BitMask();
$this->expectException(NotSingleBitException::class);
$this->expectExceptionMessage((string)(self::READ | self::WRITE));
$this->expectExceptionMessage((string) (self::READ | self::WRITE));
$bitmask->has(self::READ | self::WRITE);
}

public function testIsSetBitOutOfRange(): void
{
$bitmask = new BitMask(0, 1);
$this->expectException(OutOfRangeException::class);
$this->expectExceptionMessage((string)self::EXECUTE);
$this->expectExceptionMessage((string) self::EXECUTE);
$bitmask->has(self::EXECUTE);
}

Expand All @@ -91,15 +91,15 @@ public function testSetNotSingleBit(): void
{
$bitmask = new BitMask();
$this->expectException(NotSingleBitException::class);
$this->expectExceptionMessage((string)(self::READ | self::WRITE));
$this->expectExceptionMessage((string) (self::READ | self::WRITE));
$bitmask->set(self::READ | self::WRITE);
}

public function testSetBitOutOfRange(): void
{
$bitmask = new BitMask(0, 1);
$this->expectException(OutOfRangeException::class);
$this->expectExceptionMessage((string)self::EXECUTE);
$this->expectExceptionMessage((string) self::EXECUTE);
$bitmask->set(self::EXECUTE);
}

Expand All @@ -118,23 +118,23 @@ public function testUnsetBitNotSingleBit(): void
{
$bitmask = new BitMask(self::EXECUTE);
$this->expectException(NotSingleBitException::class);
$this->expectExceptionMessage((string)(self::READ | self::WRITE));
$this->expectExceptionMessage((string) (self::READ | self::WRITE));
$bitmask->remove(self::READ | self::WRITE);
}

public function testUnsetBitOutOfRange(): void
{
$bitmask = new BitMask(self::WRITE, 1);
$this->expectException(OutOfRangeException::class);
$this->expectExceptionMessage((string)self::EXECUTE);
$this->expectExceptionMessage((string) self::EXECUTE);
$bitmask->remove(self::EXECUTE);
}

public function testToString(): void
{
$bitmask = new BitMask(7);
assertSame('7', (string)$bitmask);
assertSame('7', (string) $bitmask);
$bitmask->set(8);
assertSame('15', (string)$bitmask);
assertSame('15', (string) $bitmask);
}
}
1 change: 0 additions & 1 deletion tests/Util/BitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

class BitsTest extends TestCase
{

public function testGetMostSignificantBit(): void
{
assertEquals(0, Bits::getMostSignificantBit(0));
Expand Down

0 comments on commit 3072607

Please sign in to comment.