Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more PHPMD Rules #95

Merged
merged 6 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .phpmd/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@
<exclude-pattern>vendor/*</exclude-pattern>

<rule ref="rulesets/cleancode.xml">
<!-- This is mainly for Laravel facade static accessors -->
<exclude name="StaticAccess" />
</rule>

<rule ref="rulesets/codesize.xml" />

<rule ref="rulesets/controversial.xml" />

<rule ref="rulesets/design.xml">
<!-- This package is designed to have many children of the healthcheck class -->
<exclude name="NumberOfChildren" />
</rule>

<rule ref="rulesets/naming.xml" />

<rule ref="rulesets/unusedcode.xml"/>
</ruleset>
8 changes: 4 additions & 4 deletions src/Checks/DatabaseHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class DatabaseHealthCheck extends HealthCheck
{
protected string $name = 'database';

protected DatabaseManager $db;
protected DatabaseManager $database;

public function __construct(DatabaseManager $db)
public function __construct(DatabaseManager $database)
{
$this->db = $db;
$this->database = $database;
}

public function status(): Status
Expand All @@ -26,7 +26,7 @@ public function status(): Status
$connection = '';
}

$pdo = $this->db->connection($connection)->getPdo();
$this->database->connection($connection)->getPdo();
} catch (Exception $e) {
return $this->problem('Could not connect to db', [
'connection' => $connection,
Expand Down
12 changes: 6 additions & 6 deletions src/Checks/HttpHealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ public function status(): Status

try {
$response = $client->get($address);
} catch (ConnectException $e) {
$badConnections->put($address, $e->getMessage());
} catch (ConnectException $exception) {
$badConnections->put($address, $exception->getMessage());

continue;
} catch (BadResponseException $e) {
$response = $e->getResponse();
} catch (Exception $e) {
$generalFailures->put($address, $this->exceptionContext($e));
} catch (BadResponseException $exception) {
$response = $exception->getResponse();
} catch (Exception $exception) {
$generalFailures->put($address, $this->exceptionContext($exception));

continue;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/CacheHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class CacheHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
21 changes: 11 additions & 10 deletions tests/Checks/DatabaseHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class DatabaseHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand All @@ -27,10 +28,10 @@ public function testShowsProblemWhenCantConnectToDb(): void
'healthcheck.database.connections' => ['default'],
]);

$db = new DatabaseManager();
$db->addConnection('default', new BadConnection());
$database = new DatabaseManager();
$database->addConnection('default', new BadConnection());

$status = (new DatabaseHealthCheck($db))->status();
$status = (new DatabaseHealthCheck($database))->status();

$this->assertTrue($status->isProblem());
}
Expand All @@ -41,10 +42,10 @@ public function testShowsOkayWhenCanConnectToDb(): void
'healthcheck.database.connections' => ['default'],
]);

$db = new DatabaseManager();
$db->addConnection('default', new HealthyConnection());
$database = new DatabaseManager();
$database->addConnection('default', new HealthyConnection());

$status = (new DatabaseHealthCheck($db))->status();
$status = (new DatabaseHealthCheck($database))->status();

$this->assertTrue($status->isOkay());
}
Expand All @@ -55,11 +56,11 @@ public function testShowsWhichConnectionFailed(): void
'healthcheck.database.connections' => ['healthy', 'bad'],
]);

$db = new DatabaseManager();
$db->addConnection('healthy', new HealthyConnection());
$db->addConnection('bad', new BadConnection());
$database = new DatabaseManager();
$database->addConnection('healthy', new HealthyConnection());
$database->addConnection('bad', new BadConnection());

$status = (new DatabaseHealthCheck($db))->status();
$status = (new DatabaseHealthCheck($database))->status();

$this->assertTrue($status->isProblem());
$this->assertSame('bad', $status->context()['connection']);
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/EnvHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class EnvHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
11 changes: 6 additions & 5 deletions tests/Checks/HttpHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class HttpHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand All @@ -34,7 +35,7 @@ public function testShowsProblemIfResponseCodeIsIncorrect(): void
'default-curl-timeout' => 1
]);

$this->app->bind(Client::class, function ($app, $args) {
$this->app->bind(Client::class, function () {
$responses = [
(new Response(500)),
];
Expand All @@ -58,7 +59,7 @@ public function testShowsProblemIfConnectionIsUnreachable(): void
'default-curl-timeout' => 1
]);

$this->app->bind(Client::class, function ($app, $args) {
$this->app->bind(Client::class, function () {
$responses = [
(new Response(500)),
];
Expand All @@ -82,7 +83,7 @@ public function testShowsProblemOnConnectException(): void
'default-curl-timeout' => 1
]);

$this->app->bind(Client::class, function ($app, $args) {
$this->app->bind(Client::class, function () {
$exceptions = [
(new ConnectException('Connection refused', new Request('GET', 'test'))),
];
Expand All @@ -106,7 +107,7 @@ public function testShowsProblemOnGeneralException(): void
'default-curl-timeout' => 1
]);

$this->app->bind(Client::class, function ($app, $args) {
$this->app->bind(Client::class, function () {
$exceptions = [
(new TooManyRedirectsException('Will not follow more than 5 redirects', new Request('GET', 'test'))),
];
Expand All @@ -130,7 +131,7 @@ public function testShowsOkayIfAllConnectionsAreReachable(): void
'default-curl-timeout' => 1,
]);

$this->app->bind(Client::class, function ($app, $args) {
$this->app->bind(Client::class, function () {
$responses = [
(new Response(200)),
];
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/LogHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class LogHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
12 changes: 6 additions & 6 deletions tests/Checks/MigrationUpToDateHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MigrationUpToDateHealthCheckTest extends TestCase

protected MockObject $migratorMock;

protected MockObject $migrationRepositoryMock;
protected MockObject $migrationRepository;


public function prepare(): void
Expand All @@ -27,13 +27,13 @@ public function prepare(): void
->disableOriginalConstructor()
->getMock();

$this->migrationRepositoryMock = $this->getMockBuilder(MigrationRepositoryInterface::class)
$this->migrationRepository = $this->getMockBuilder(MigrationRepositoryInterface::class)
->disableOriginalConstructor()
->getMock();

$this->migratorMock->expects(($this->any()))
->method('getRepository')
->willReturn($this->migrationRepositoryMock);
->willReturn($this->migrationRepository);

$this->migratorMock->expects($this->any())
->method('repositoryExists')
Expand All @@ -53,7 +53,7 @@ public function testCanReturnFalseWhenSchemaIsOutdated(): void
'missing_migration.php' => 2
]);

$this->migrationRepositoryMock->expects($this->once())
$this->migrationRepository->expects($this->once())
->method('getRan')
->willReturn([]);

Expand All @@ -75,7 +75,7 @@ public function testCanReturnFalseWhenRanMigrationCouldNotBeRetrieved(): void
->method('repositoryExists')
->willReturn(false);

$this->migrationRepositoryMock->expects($this->any())
$this->migrationRepository->expects($this->any())
->method('getRan')
->willReturn([]);

Expand All @@ -91,7 +91,7 @@ public function testCanReturnTrueWhenMigrationsAreUpToDate(): void
'executed_migration.php' => 2
]);

$this->migrationRepositoryMock->expects($this->any())
$this->migrationRepository->expects($this->any())
->method('getRan')
->willReturn([
'executed_migration.php'
Expand Down
16 changes: 14 additions & 2 deletions tests/Checks/PackageSecurityHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Closure;
use Exception;
use Illuminate\Foundation\Application;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Mockery;
use Mockery\MockInterface;
use Tests\Stubs\Checks\PackageSecurityHealthCheck as StubPackageSecurityHealthCheck;
Expand All @@ -15,6 +17,7 @@
class PackageSecurityHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand All @@ -30,9 +33,18 @@ public function getPackageProviders($app): array
* @param \Closure|null $mock
* @return MockInterface
*/
protected function partialMock($abstract, Closure $mock = null): MockInterface
protected function partialMock($abstract, ?Closure $mock = null): MockInterface
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
/**
* @var Collection<int, string|Closure,null> $arguments
*/
$arguments = collect([
$abstract,
$mock,
])
->filter();

return $this->instance($abstract, Mockery::mock(...$arguments)->makePartial());
}

public function testShowsProblemIfRequiredPackageNotLoaded()
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/RedisHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class RedisHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/SchedulerHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class SchedulerHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Checks/StorageHealthCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class StorageHealthCheckTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Commands/HealthCheckMakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class HealthCheckMakeCommandTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Controllers/HealthCheckControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class HealthCheckControllerTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
1 change: 1 addition & 0 deletions tests/Controllers/PingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class PingControllerTest extends TestCase
{
/**
* @inheritDoc
* @param Application $app
* @return array<int, class-string>
*/
Expand Down
Loading