Skip to content

Commit

Permalink
Setup psalm (#8)
Browse files Browse the repository at this point in the history
* Install psalm

* Add psalm config

* Fix linting errors
  • Loading branch information
slavcodev authored Feb 18, 2020
1 parent 877fa68 commit 409076d
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 115 deletions.
3 changes: 3 additions & 0 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<files>
</files>
108 changes: 108 additions & 0 deletions .psalm/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?xml version="1.0"?>
<!--
~ This source file is proprietary and part of Rebilly.
~
~ (c) Rebilly SRL
~ Rebilly Ltd.
~ Rebilly Inc.
~
~ @see https://www.rebilly.com
-->

<psalm
totallyTyped="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"
allowPhpStormGenerics="true"
cacheDirectory=".psalm/cache"
errorBaseline=".psalm/baseline.xml"
>
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType/>
<InvalidDocblock/>
<MisplacedRequiredParam/>
<MissingConstructor/>
<DocblockTypeContradiction/>
<UnresolvableInclude/>
<RawObjectIteration/>
<InvalidStringClass/>
<MismatchingDocblockReturnType/>
<UndefinedGlobalVariable/>
<FalsableReturnStatement/>
<InvalidFalsableReturnType/>
<UnusedFunctionCall/>
<PossiblyUnusedParam/>
<MismatchingDocblockParamType/>
<UndefinedClass/>
<UnusedParam/>
<InvalidReturnStatement/>
<RedundantCondition/>
<PossiblyUndefinedVariable/>
<InternalMethod/>
<InternalProperty/>
<InternalClass/>
<MissingClosureReturnType/>
<MissingClosureParamType/>
<UndefinedThisPropertyAssignment/>
<DuplicateArrayKey/>
<PropertyNotSetInConstructor/>
<PossiblyUnusedProperty/>

<!-- Temporary disabled -->

<MissingReturnType errorLevel="suppress"/>
<MissingParamType errorLevel="suppress"/>
<PossiblyNullArgument errorLevel="suppress"/>
<PossiblyNullArrayOffset errorLevel="suppress"/>
<PossiblyNullArrayAccess errorLevel="suppress"/>
<PossiblyNullPropertyAssignmentValue errorLevel="suppress"/>
<PossiblyNullOperand errorLevel="suppress"/>
<NullArgument errorLevel="suppress"/>
<InvalidOperand errorLevel="suppress"/>
<RedundantConditionGivenDocblockType errorLevel="suppress"/>
<InvalidNullableReturnType errorLevel="suppress"/>
<InvalidPropertyAssignmentValue errorLevel="suppress"/>
<PossiblyInvalidMethodCall errorLevel="suppress"/>
<UnusedVariable errorLevel="suppress"/>
<PossiblyNullReference errorLevel="suppress"/>
<MoreSpecificImplementedParamType errorLevel="suppress"/>
<TypeDoesNotContainType errorLevel="suppress"/>
<PossiblyNullPropertyFetch errorLevel="suppress"/>
<InvalidPropertyFetch errorLevel="suppress"/>
<InvalidReturnType errorLevel="suppress"/>
<UndefinedPropertyFetch errorLevel="suppress"/>
<InvalidArgument errorLevel="suppress"/>
<PossiblyUndefinedMethod errorLevel="suppress"/>
<DocblockTypeContradiction errorLevel="suppress"/>
<NullableReturnStatement errorLevel="suppress"/>
<InvalidScalarArgument errorLevel="suppress"/>
<InvalidMethodCall errorLevel="suppress"/>
<UndefinedMethod errorLevel="suppress"/>
<InvalidArrayOffset errorLevel="suppress"/>

<!-- No needed for library -->

<MissingPropertyType errorLevel="suppress"/>
<UnusedClass errorLevel="suppress"/>
<UnusedMethod errorLevel="suppress"/>
<DeprecatedMethod errorLevel="suppress"/>
<DeprecatedProperty errorLevel="suppress"/>
<DeprecatedClass errorLevel="suppress"/>
<DeprecatedConstant errorLevel="suppress"/>
<DeprecatedFunction errorLevel="suppress"/>
<DeprecatedInterface errorLevel="suppress"/>
<DeprecatedTrait errorLevel="suppress"/>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.8",
"phpunit/phpunit": "^7.5",
"php-coveralls/php-coveralls": "^2.1"
"php-coveralls/php-coveralls": "^2.1",
"vimeo/psalm": "^3.9",
"psalm/plugin-phpunit": "^0.9.0"
},

"autoload": {
Expand All @@ -36,10 +38,5 @@
},

"suggest": {},

"scripts": {
"cs": "php-cs-fixer fix --verbose --dry-run --ansi",
"cs-fix": "php-cs-fixer fix --verbose --ansi",
"test": "phpunit"
}
"scripts": {}
}
2 changes: 1 addition & 1 deletion src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct(int $amount, Currency $currency)
* currency's number of fractional digits.
*
* @param string $value
* @param string $currency
* @param Currency|string $currency
*
* @throws InvalidArgumentException
*
Expand Down
48 changes: 24 additions & 24 deletions tests/CurrencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,45 @@ public function testCanBeConstructedFromUppercaseString(): void
{
$c = new Currency('EUR');

$this->assertSame('EUR', $c->getCurrencyCode());
$this->assertSame(2, $c->getDefaultFractionDigits());
$this->assertSame('Euro', $c->getDisplayName());
$this->assertSame(978, $c->getNumericCode());
$this->assertSame(100, $c->getSubUnit());
$this->assertSame('', $c->getSign());
$this->assertFalse($c->isDeprecated());
self::assertSame('EUR', $c->getCurrencyCode());
self::assertSame(2, $c->getDefaultFractionDigits());
self::assertSame('Euro', $c->getDisplayName());
self::assertSame(978, $c->getNumericCode());
self::assertSame(100, $c->getSubUnit());
self::assertSame('', $c->getSign());
self::assertFalse($c->isDeprecated());
}

public function testCanBeConstructedFromLowercaseString(): void
{
$c = new Currency('eur');

$this->assertSame('EUR', $c->getCurrencyCode());
self::assertSame('EUR', $c->getCurrencyCode());
}

public function testCustomCurrencyCanBeRegistered(): void
{
Currency::addCurrency('BTC', 'Bitcoin', 999, 4, 1000);
$c = new Currency('BTC');

$this->assertSame('BTC', $c->getCurrencyCode());
self::assertSame('BTC', $c->getCurrencyCode());
}

public function testRegisteredCurrenciesCanBeAccessed(): void
{
$currencies = Currency::getCurrencies();

$this->assertInternalType('array', $currencies);
$this->assertArrayHasKey('EUR', $currencies);
$this->assertInternalType('array', $currencies['EUR']);
$this->assertArrayHasKey('display_name', $currencies['EUR']);
$this->assertArrayHasKey('numeric_code', $currencies['EUR']);
$this->assertArrayHasKey('default_fraction_digits', $currencies['EUR']);
$this->assertArrayHasKey('sub_unit', $currencies['EUR']);
$this->assertArrayHasKey('deprecated', $currencies['EUR']);
self::assertInternalType('array', $currencies);
self::assertArrayHasKey('EUR', $currencies);
self::assertInternalType('array', $currencies['EUR']);
self::assertArrayHasKey('display_name', $currencies['EUR']);
self::assertArrayHasKey('numeric_code', $currencies['EUR']);
self::assertArrayHasKey('default_fraction_digits', $currencies['EUR']);
self::assertArrayHasKey('sub_unit', $currencies['EUR']);
self::assertArrayHasKey('deprecated', $currencies['EUR']);

// check that getCurrencies() method doesn't return deprecated currencies
$this->assertArrayNotHasKey('BYR', $currencies);
self::assertArrayNotHasKey('BYR', $currencies);
}

public function testGetCurrenciesIncludingDeprecated(): void
Expand All @@ -90,27 +90,27 @@ public function testGetCurrenciesIncludingDeprecated(): void
$activeCurrency = 'EUR';
$deprecatedCurrency = 'BYR';

$this->assertArrayHasKey($activeCurrency, $currencies);
$this->assertArrayHasKey($deprecatedCurrency, $currencies);
self::assertArrayHasKey($activeCurrency, $currencies);
self::assertArrayHasKey($deprecatedCurrency, $currencies);
}

public function testCanBeCastToString(): void
{
$this->assertSame('EUR', (string) new Currency('EUR'));
self::assertSame('EUR', (string) new Currency('EUR'));
}

public function testCanCreateByNumCode(): void
{
$this->assertSame('EUR', Currency::fromNumericCode(978)->getCurrencyCode());
self::assertSame('EUR', Currency::fromNumericCode(978)->getCurrencyCode());
}

public function testDeprecation(): void
{
$activeCurrency = new Currency('EUR');
$deprecatedCurrency = new Currency('BYR');

$this->assertFalse($activeCurrency->isDeprecated());
$this->assertTrue($deprecatedCurrency->isDeprecated());
self::assertFalse($activeCurrency->isDeprecated());
self::assertTrue($deprecatedCurrency->isDeprecated());
}

public function testExceptionIsRaisedForInvalidNumCode(): void
Expand Down
20 changes: 10 additions & 10 deletions tests/Exchange/CurrencyPairTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function testCanBeConstructedFromDifferentCurrencies()
$b = new Currency('USD');
$c = new CurrencyPair($a, $b);

$this->assertInstanceOf(CurrencyPair::class, $c);
self::assertSame($a, $c->getBaseCurrency());
self::assertSame($b, $c->getQuoteCurrency());

return $c;
}
Expand All @@ -33,7 +34,7 @@ public function testCanBeConstructedFromDifferentCurrencies()
*/
public function testCanBeSerialized(CurrencyPair $pair): void
{
$this->assertSame('{"baseCurrency":"EUR","quoteCurrency":"USD"}', json_encode($pair));
self::assertSame('{"baseCurrency":"EUR","quoteCurrency":"USD"}', json_encode($pair));
}

/**
Expand All @@ -43,7 +44,7 @@ public function testCanBeSerialized(CurrencyPair $pair): void
*/
public function testCanBeCastToString(CurrencyPair $c): void
{
$this->assertSame('EUR/USD', (string) $c);
self::assertSame('EUR/USD', (string) $c);
}

/**
Expand All @@ -53,7 +54,7 @@ public function testCanBeCastToString(CurrencyPair $c): void
*/
public function testCanGetBaseCurrency(CurrencyPair $c): void
{
$this->assertSame('EUR', $c->getBaseCurrency()->getCurrencyCode());
self::assertSame('EUR', $c->getBaseCurrency()->getCurrencyCode());
}

/**
Expand All @@ -63,7 +64,7 @@ public function testCanGetBaseCurrency(CurrencyPair $c): void
*/
public function testCanGetTargetCurrency(CurrencyPair $c): void
{
$this->assertSame('USD', $c->getQuoteCurrency()->getCurrencyCode());
self::assertSame('USD', $c->getQuoteCurrency()->getCurrencyCode());
}

/**
Expand All @@ -74,7 +75,7 @@ public function testCanGetTargetCurrency(CurrencyPair $c): void
public function testEquals(CurrencyPair $c): void
{
$cp = new CurrencyPair(new Currency('EUR'), new Currency('USD'));
$this->assertTrue($c->equals($cp));
self::assertTrue($c->equals($cp));
}

/**
Expand All @@ -87,8 +88,7 @@ public function testCanGetRate(CurrencyPair $c): void
$rateProvider = new InMemoryRateProvider(['EUR/USD' => 1.09], new DateTime());
$rate = $c->getRate($rateProvider);

$this->assertInstanceOf(Rate::class, $rate);
$this->assertSame(1.09, $rate->getRatio());
self::assertSame(1.09, $rate->getRatio());
}

/**
Expand All @@ -99,7 +99,7 @@ public function testCanGetRate(CurrencyPair $c): void
public function testInverse(CurrencyPair $c): void
{
$inverted = $c->getInverse();
$this->assertFalse($inverted->equals($c));
$this->assertTrue($inverted->equals(new CurrencyPair(new Currency('USD'), new Currency('EUR'))));
self::assertFalse($inverted->equals($c));
self::assertTrue($inverted->equals(new CurrencyPair(new Currency('USD'), new Currency('EUR'))));
}
}
23 changes: 13 additions & 10 deletions tests/Exchange/RateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ public function testExceptionIsRaisedForInvalidConstructorArgument(): void

public function testCanBeConstructed()
{
$date = $this->getRateDate();
$cp = new CurrencyPair(new Currency('USD'), new Currency('EUR'));
$rate = new Rate($cp, $this->getRateDate(), 0.92);
$rate = new Rate($cp, $date, 0.92);

$this->assertInstanceOf(Rate::class, $rate);
self::assertSame($cp, $rate->getCurrencyPair());
self::assertSame($date, $rate->getDate());
self::assertSame(0.92, $rate->getRatio());

return $rate;
}
Expand All @@ -35,7 +38,7 @@ public function testCanBeConstructed()
*/
public function testCanBeSerialized(Rate $rate): void
{
$this->assertSame(
self::assertSame(
'{"baseCurrency":"USD","quoteCurrency":"EUR","date":"' . $this->getRateDate()->format('c') . '","ratio":0.92}',
json_encode($rate)
);
Expand All @@ -48,7 +51,7 @@ public function testCanBeSerialized(Rate $rate): void
*/
public function testCanGetRatio(Rate $rate): void
{
$this->assertSame(0.92, $rate->getRatio());
self::assertSame(0.92, $rate->getRatio());
}

/**
Expand All @@ -58,7 +61,7 @@ public function testCanGetRatio(Rate $rate): void
*/
public function testCanGetCurrencyPair(Rate $rate): void
{
$this->assertInstanceOf(CurrencyPair::class, $rate->getCurrencyPair());
self::assertInstanceOf(CurrencyPair::class, $rate->getCurrencyPair());
}

/**
Expand All @@ -68,7 +71,7 @@ public function testCanGetCurrencyPair(Rate $rate): void
*/
public function testCanGetDate(Rate $rate): void
{
$this->assertInstanceOf(DateTime::class, $rate->getDate());
self::assertInstanceOf(DateTime::class, $rate->getDate());
}

/**
Expand All @@ -93,8 +96,8 @@ public function testCanConvertMoney(Rate $rate): void
{
$money = new Money(1000, new Currency('USD'));
$newMoney = $rate->convert($money);
$this->assertSame(920, $newMoney->getAmount());
$this->assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode());
self::assertSame(920, $newMoney->getAmount());
self::assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode());
}

/**
Expand All @@ -109,8 +112,8 @@ public function testCanConvertMoneyWithMarkup(Rate $rate): void
$newMoney = $rate->convert($money)->multiply($markupBips / 10000 + 1);

// the straight conversion without markup is EUR 9.20
$this->assertSame(966, $newMoney->getAmount());
$this->assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode());
self::assertSame(966, $newMoney->getAmount());
self::assertSame('EUR', $newMoney->getCurrency()->getCurrencyCode());
}

private function getRateDate(): DateTime
Expand Down
Loading

0 comments on commit 409076d

Please sign in to comment.