Skip to content

Commit

Permalink
Throw exception upon failure instead of arbitrary default value (#21)
Browse files Browse the repository at this point in the history
The reasoning is that depending of the context, one may want to apply a different arbitrary default value. It is easier to do that by wrapping the call in a try/catch catching a specific exception rather than getting the value directly (since you wouldn't know if you get the right value found, or the default one).
  • Loading branch information
theofidry authored Dec 4, 2022
1 parent 6b9a0cc commit f01ffc5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/CpuCoreCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ final class CpuCoreCounter
private int $count;

/**
* @param list<CpuCoreFinder> $finders
* @param list<CpuCoreFinder>|null $finders
*/
public function __construct(?array $finders = null)
{
$this->finders = $finders ?? self::getDefaultFinders();
}

/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
public function getCount(): int
Expand All @@ -47,6 +49,8 @@ public function getCount(): int
}

/**
* @throws NumberOfCpuCoreNotFound
*
* @return positive-int
*/
private function findCount(): int
Expand All @@ -63,7 +67,7 @@ private function findCount(): int
}
}

return 2;
throw NumberOfCpuCoreNotFound::create();
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/NumberOfCpuCoreNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Fidry CPUCounter Config package.
*
* (c) Théo FIDRY <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Fidry\CpuCounter;

use RuntimeException;

final class NumberOfCpuCoreNotFound extends RuntimeException
{
public static function create(): self
{
return new self(
'Could not find the number of CPU cores available.',
);
}
}
29 changes: 24 additions & 5 deletions tests/CpuCoreCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

namespace Fidry\CpuCounter\Test;

use Exception;
use Fidry\CpuCounter\CpuCoreCounter;
use Fidry\CpuCounter\NumberOfCpuCoreNotFound;
use PHPUnit\Framework\TestCase;
use function get_class;
use function is_int;

/**
* @covers \Fidry\CpuCounter\CpuCoreCounter
Expand All @@ -34,23 +38,38 @@ public function test_it_can_get_the_number_of_cpu_cores(): void
* @dataProvider cpuCoreFinderProvider
*
* @param list<CpuCoreCounter> $finders
* @param int|Exception $expected
*/
public function test_it_can_get_the_number_of_cpu_cores_based_on_the_registered_finders(
array $finders,
int $expected
$expected
): void {
$counter = new CpuCoreCounter($finders);

$actual = $counter->getCount();
if (is_int($expected)) {
$actual = $counter->getCount();

self::assertSame($expected, $actual);
self::assertSame($expected, $actual);

return;
}

// Sanity check
self::assertTrue($expected instanceof Exception);

$this->expectException(get_class($expected));
$this->expectExceptionMessage($expected->getMessage());

$counter->getCount();
}

public static function cpuCoreFinderProvider(): iterable
{
$defaultException = NumberOfCpuCoreNotFound::create();

yield 'no finder' => [
[],
2,
$defaultException,
];

yield 'single finder finds a value' => [
Expand All @@ -64,7 +83,7 @@ public static function cpuCoreFinderProvider(): iterable
[
new DummyCpuCoreFinder(null),
],
2,
$defaultException,
];

yield 'multiple finders find a value' => [
Expand Down

0 comments on commit f01ffc5

Please sign in to comment.