Passed
Push — main ( 7af4bb...131f20 )
by Rafael
58:13
created

Dispatcher   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 34
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A processFolder() 0 18 4
1
<?php
2
3
/* Copyright (C) 2024      Rafael San José      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace DoliCore\Tools;
20
21
use Alxarafe\Tools\Debug;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, DoliCore\Tools\Debug. 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...
22
use Alxarafe\Tools\Dispatcher as DispatcherBase;
23
24
class Dispatcher extends DispatcherBase
25
{
26
    /**
27
     * Process controller paths imported from Dolibarr. Try to locate it as a
28
     * modern controller first.
29
     *
30
     * This code is duplicated in Core/Dispatcher.
31
     * It can be refactored by creating an array with the possible "className" and
32
     * "filename", and looping through them all from Core/Dispatcher.
33
     * In that case, Dolibarr/Dispatcher would only have to add the new routes to
34
     * search for your drivers.
35
     *
36
     * @param string $module
37
     * @param string $controller
38
     * @return bool
39
     */
40
    protected static function processFolder(string $module, string $controller): bool
41
    {
42
        if (parent::processFolder($module, $controller)) {
43
            return true;
44
        }
45
        $className = 'DoliModules\\' . $module . '\\Controller\\' . $controller;
46
        $filename = realpath(constant('BASE_PATH') . '/../Dolibarr/Modules/' . $module . '/Controller/' . $controller . '.php');
47
        Debug::message('Filename: ' . $filename);
48
        Debug::message('Class: ' . $className);
49
        if (!file_exists($filename)) {
50
            return false;
51
        }
52
        $controller = new $className();
53
        if ($controller === null) {
54
            return false;
55
        }
56
        $controller->index();
57
        return true;
58
    }
59
}
60