Total Complexity | 5 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Coverage | 0% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
20 | trait Slug |
||
21 | { |
||
22 | use Options; |
||
23 | use AbstractBehavior; |
||
24 | use AbstractInjectable; |
||
25 | |||
26 | public Transformable $slugBehavior; |
||
27 | |||
28 | /** |
||
29 | * Initializing Slug |
||
30 | */ |
||
31 | public function initializeSlug(?array $options = null): void |
||
32 | { |
||
33 | $options ??= $this->getOptionsManager()->get('slug') ?? []; |
||
34 | $field = $options['field'] ?? 'slug'; |
||
35 | |||
36 | $security = $this->getDI()->get('security'); |
||
37 | assert($security instanceof Security); |
||
38 | |||
39 | $this->setSlugBehavior(new Transformable([ |
||
40 | 'beforeValidation' => [ |
||
41 | $field => function (Model $model, $field) { |
||
42 | $value = $model->readAttribute($field); |
||
43 | return $value && is_string($value) ? \Zemit\Utils\Slug::generate($value) : $value; |
||
44 | }, |
||
45 | ], |
||
46 | ])); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Set Slug Behavior |
||
51 | */ |
||
52 | public function setSlugBehavior(Transformable $slugBehavior): void |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Get Slug Behavior |
||
60 | */ |
||
61 | public function getSlugBehavior(): Transformable |
||
66 |
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: