Issues (480)

src/Event/EventModel.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 *
4
 */
5
6
namespace Mvc5\Event;
7
8
use Mvc5\Signal;
9
10
use function is_string;
11
use function key;
12
13
use const Mvc5\EVENT;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Mvc5\Event\EVENT. 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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
The constant Mvc5\EVENT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
trait EventModel
16
{
17
    /**
18
     *
19
     */
20
    use Model;
21
22
    /**
23
     * @return array
24
     */
25 3
    protected function args() : array
26
    {
27
        return [
28 3
            EVENT => $this
29
        ];
30
    }
31
32
    /**
33
     * @param callable $callable
34
     * @param array $args
35
     * @param callable|null $callback
36
     * @return mixed
37
     * @throws \Throwable
38
     */
39 24
    protected function signal(callable $callable, array $args = [], callable $callback = null)
40
    {
41 24
        return Signal::emit($callable, $args, $callback);
42
    }
43
44
    /**
45
     * @param callable $callable
46
     * @param array $args
47
     * @param callable|null $callback
48
     * @return mixed
49
     * @throws \Throwable
50
     */
51 3
    function __invoke(callable $callable, array $args = [], callable $callback = null)
52
    {
53 3
        return $this->signal(
54 3
            $callable, !$args ? $this->args() : (!is_string(key($args)) ? $args : $this->args() + $args), $callback
55
        );
56
    }
57
}
58