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

Phpstan lvl 1 #973

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 4 additions & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ jobs:
- name: Execute tests
run: |
vendor/bin/phpunit
vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver
vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver

- name: Run PHPStan
run: composer analyse
17 changes: 14 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,27 @@
"phpunit/phpunit": "^9.6|^10.5|^11.0",
"mockery/mockery": "^1.0",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0",
"laravel/legacy-factories": "*"
"laravel/legacy-factories": "*",
"phpstan/phpstan": "^1.12",
"phpstan/phpstan-phpunit": "*",
"larastan/larastan": "*",
"phpstan/phpstan-mockery": "^1.1",
"phpstan/phpstan-strict-rules": "^1.6"
},
"scripts": {
"analyse": "vendor/bin/phpstan analyse --memory-limit=1G",
"test": "vendor/bin/phpunit"
},
"autoload": {
"psr-4": {
"OwenIt\\Auditing\\": "src/"
"OwenIt\\Auditing\\": "src/",
"OwenIt\\Auditing\\Database\\Factories\\": "database/factories/"
}
},
"autoload-dev": {
"psr-4": {
"OwenIt\\Auditing\\Tests\\": "tests/"
"OwenIt\\Auditing\\Tests\\": "tests/",
"OwenIt\\Auditing\\Tests\\Database\\Factories\\": "tests/database/factories/"
}
},
"suggest": {
Expand Down
42 changes: 42 additions & 0 deletions database/factories/AuditFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace OwenIt\Auditing\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use OwenIt\Auditing\Models\Audit;
use Illuminate\Support\Facades\Config;
use OwenIt\Auditing\Tests\Models\Article;
use OwenIt\Auditing\Tests\Models\User;
/**
* @extends Factory<Audit>
*/
class AuditFactory extends Factory
{
protected $model = Audit::class;

/**
* @return array<string, mixed>
*/
public function definition(): array
{
$morphPrefix = Config::get('audit.user.morph_prefix', 'user');

return [
$morphPrefix . '_id' => function () {
return User::factory()->create()->id;
},
$morphPrefix . '_type' => User::class,
'event' => 'updated',
'auditable_id' => function () {
return Article::factory()->create()->id;
},
'auditable_type' => Article::class,
'old_values' => [],
'new_values' => [],
'url' => fake()->url,
'ip_address' => fake()->ipv4,
'user_agent' => fake()->userAgent,
'tags' => implode(',', fake()->words(4)),
];
}
}
22 changes: 22 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
includes:
- vendor/larastan/larastan/extension.neon
- vendor/phpstan/phpstan-mockery/extension.neon
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
level: 1
paths:
- ./config
- ./database
- ./src
- ./tests
exceptions:
implicitThrows: false
check:
tooWideThrowType: true
excludePaths:
- tests/database/migrations
services:
-
class: PHPStan\Rules\Classes\RequireParentConstructCallRule
tags:
- phpstan.rules.rule
14 changes: 8 additions & 6 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="true"
verbose="true"
>
<source>
<include>
<directory suffix=".php">src</directory>
<directory suffix=".php">config</directory>
<directory suffix=".php">database</directory>
</include>
</source>
<testsuites>
<testsuite name="Auditing Test Suite">
<directory suffix="Test.php">./tests</directory>
Expand All @@ -27,7 +29,7 @@
<group>command-line-url-resolver</group>
</exclude>
</groups>
<coverage processUncoveredFiles="true">
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
Expand Down
16 changes: 13 additions & 3 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ protected function resolveAuditExclusions()
$this->excludedAttributes[] = $this->getUpdatedAtColumn();
}
if (in_array(SoftDeletes::class, class_uses_recursive(get_class($this)))) {
assert(method_exists($this, 'getDeletedAtColumn'));
$this->excludedAttributes[] = $this->getDeletedAtColumn();
}
}
Expand Down Expand Up @@ -707,7 +708,6 @@ public function transitionTo(Contracts\Audit $audit, bool $old = false): Contrac
* @param array $columns
* @param \Closure|null $callback
* @return void
* @throws AuditingException
*/
public function auditAttach(string $relationName, $id, array $attributes = [], $touch = true, $columns = ['*'], $callback = null)
{
Expand All @@ -733,7 +733,6 @@ public function auditAttach(string $relationName, $id, array $attributes = [], $
* @param array $columns
* @param \Closure|null $callback
* @return int
* @throws AuditingException
*/
public function auditDetach(string $relationName, $ids = null, $touch = true, $columns = ['*'], $callback = null)
{
Expand Down Expand Up @@ -761,7 +760,6 @@ public function auditDetach(string $relationName, $ids = null, $touch = true, $c
* @param array $columns
* @param \Closure|null $callback
* @return array
* @throws AuditingException
*/
public function auditSync(string $relationName, $ids, $detaching = true, $columns = ['*'], $callback = null)
{
Expand Down Expand Up @@ -854,13 +852,25 @@ private function dispatchRelationAuditEvent($relationName, $event, $old, $new)
$this->isCustomEvent = false;
}

/**
* @param string $relationName
* @param string $methodName
* @return void
* @throws AuditingException
*/
private function validateRelationshipMethodExistence(string $relationName, string $methodName): void
{
if (!method_exists($this, $relationName) || !method_exists($this->{$relationName}(), $methodName)) {
throw new AuditingException("Relationship $relationName was not found or does not support method $methodName");
}
}

/**
* @param BelongsToMany $relation
* @param \Closure $closure
* @return void
* @throws AuditingException
*/
private function applyClosureToRelationship(BelongsToMany $relation, \Closure $closure): void
{
try {
Expand Down
3 changes: 2 additions & 1 deletion src/AuditableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ protected function dispatchAudit(Auditable $model)

$model->preloadResolverData();
if (!Config::get('audit.queue.enable', false)) {
return Auditor::execute($model);
Auditor::execute($model);
return;
}

if (!$this->fireDispatchingAuditEvent($model)) {
Expand Down
8 changes: 8 additions & 0 deletions src/Models/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace OwenIt\Auditing\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use OwenIt\Auditing\Database\Factories\AuditFactory;

/**
* @property string $tags
Expand All @@ -15,6 +17,7 @@
class Audit extends Model implements \OwenIt\Auditing\Contracts\Audit
{
use \OwenIt\Auditing\Audit;
use HasFactory;

/**
* {@inheritdoc}
Expand All @@ -41,4 +44,9 @@ public function getSerializedDate($date)
{
return $this->serializeDate($date);
}

public static function newFactory(): AuditFactory
{
return new AuditFactory();
}
}
13 changes: 0 additions & 13 deletions tests/AuditingTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,4 @@ protected function getPackageProviders($app)
AuditingServiceProvider::class,
];
}

/**
* Locate the Illuminate testing class. It changed namespace with v7
* @see https://readouble.com/laravel/7.x/en/upgrade.html
* @return class-string<\Illuminate\Foundation\Testing\Assert|\Illuminate\Testing\Assert>
*/
public static function Assert(): string
{
if (class_exists('Illuminate\Foundation\Testing\Assert')) {
return '\Illuminate\Foundation\Testing\Assert';
}
return '\Illuminate\Testing\Assert';
}
}
Loading