1 | <?php |
||||
2 | |||||
3 | namespace OwenIt\Auditing; |
||||
4 | |||||
5 | use OwenIt\Auditing\Contracts\Auditable; |
||||
0 ignored issues
–
show
|
|||||
6 | use OwenIt\Auditing\Facades\Auditor; |
||||
0 ignored issues
–
show
This use statement conflicts with another class in this namespace,
OwenIt\Auditing\Auditor . Consider defining an alias.
Let?s assume that you have a directory layout like this: .
|-- OtherDir
| |-- Bar.php
| `-- Foo.php
`-- SomeDir
`-- Foo.php
and let?s assume the following content of // Bar.php
namespace OtherDir;
use SomeDir\Foo; // This now conflicts the class OtherDir\Foo
If both files PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as // Bar.php
namespace OtherDir;
use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
![]() |
|||||
7 | |||||
8 | class AuditableObserver |
||||
9 | { |
||||
10 | /** |
||||
11 | * Is the model being restored? |
||||
12 | * |
||||
13 | * @var bool |
||||
14 | */ |
||||
15 | public static $restoring = false; |
||||
16 | |||||
17 | /** |
||||
18 | * Handle the retrieved event. |
||||
19 | * |
||||
20 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
21 | * |
||||
22 | * @return void |
||||
23 | */ |
||||
24 | 20 | public function retrieved(Auditable $model) |
|||
25 | { |
||||
26 | 20 | Auditor::execute($model->setAuditEvent('retrieved')); |
|||
27 | 20 | } |
|||
28 | |||||
29 | /** |
||||
30 | * Handle the created event. |
||||
31 | * |
||||
32 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
33 | * |
||||
34 | * @return void |
||||
35 | */ |
||||
36 | 50 | public function created(Auditable $model) |
|||
37 | { |
||||
38 | 50 | Auditor::execute($model->setAuditEvent('created')); |
|||
39 | 48 | } |
|||
40 | |||||
41 | /** |
||||
42 | * Handle the updated event. |
||||
43 | * |
||||
44 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
45 | * |
||||
46 | * @return void |
||||
47 | */ |
||||
48 | 6 | public function updated(Auditable $model) |
|||
49 | { |
||||
50 | // Ignore the updated event when restoring |
||||
51 | 6 | if (!static::$restoring) { |
|||
52 | 5 | Auditor::execute($model->setAuditEvent('updated')); |
|||
53 | } |
||||
54 | 6 | } |
|||
55 | |||||
56 | /** |
||||
57 | * Handle the deleted event. |
||||
58 | * |
||||
59 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
60 | * |
||||
61 | * @return void |
||||
62 | */ |
||||
63 | 3 | public function deleted(Auditable $model) |
|||
64 | { |
||||
65 | 3 | Auditor::execute($model->setAuditEvent('deleted')); |
|||
66 | 3 | } |
|||
67 | |||||
68 | /** |
||||
69 | * Handle the restoring event. |
||||
70 | * |
||||
71 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
72 | * |
||||
73 | * @return void |
||||
74 | */ |
||||
75 | 2 | public function restoring(Auditable $model) |
|||
0 ignored issues
–
show
The parameter
$model is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
76 | { |
||||
77 | // When restoring a model, an updated event is also fired. |
||||
78 | // By keeping track of the main event that took place, |
||||
79 | // we avoid creating a second audit with wrong values |
||||
80 | 2 | static::$restoring = true; |
|||
81 | 2 | } |
|||
82 | |||||
83 | /** |
||||
84 | * Handle the restored event. |
||||
85 | * |
||||
86 | * @param \OwenIt\Auditing\Contracts\Auditable $model |
||||
87 | * |
||||
88 | * @return void |
||||
89 | */ |
||||
90 | 2 | public function restored(Auditable $model) |
|||
91 | { |
||||
92 | 2 | Auditor::execute($model->setAuditEvent('restored')); |
|||
93 | |||||
94 | // Once the model is restored, we need to put everything back |
||||
95 | // as before, in case a legitimate update event is fired |
||||
96 | 2 | static::$restoring = false; |
|||
97 | 2 | } |
|||
98 | } |
||||
99 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: