Issues (29)

src/RepositoryServiceProvider.php (2 issues)

1
<?php
2
3
namespace Shamaseen\Repository\Generator;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\ServiceProvider;
7
use Illuminate\Support\Str;
8
use RecursiveDirectoryIterator;
9
use RecursiveIteratorIterator;
10
use RecursiveRegexIterator;
11
use RegexIterator;
12
use Shamaseen\Repository\Generator\Utility\ContractInterface;
13
14
/**
15
 * Class RepositoryServiceProvider.
16
 */
17
class RepositoryServiceProvider extends ServiceProvider
18
{
19
    /**
20
     * Indicates if loading of the provider is deferred.
21
     *
22
     * @var bool
23
     */
24
    protected $defer = true;
25
26
    /**
27
     * All of the container bindings that should be registered.
28
     *
29
     * @var array
30
     */
31
    public $bindings = [];
32
    protected $providers = [];
33
34
    /**
35
     * RepositoryServiceProvider constructor.
36
     *
37
     * @param $app
38
     */
39
    public function __construct($app)
40
    {
41
        parent::__construct($app);
42
43
        if (null === $this->app['config']->get('repository')) {
44
            $this->app['config']->set('repository', require __DIR__ . '/config/repository.php');
45
        }
46
        $interfaces = Str::plural(Config::get('repository.interface'));
47
        $repositories = Str::plural(Config::get('repository.repository'));
48
        $interface = Config::get('repository.interface');
49
        $repository = Config::get('repository.repository');
50
51
        $contractsFolder = Config::get('repository.app_path') . '/' . $interfaces;
52
53
        if (is_dir($contractsFolder)) {
54
            $directory = new RecursiveDirectoryIterator($contractsFolder);
55
            $iterator = new RecursiveIteratorIterator($directory);
56
            $regex = new RegexIterator($iterator, '/^.+\.php$/i', RecursiveRegexIterator::GET_MATCH);
57
            foreach ($regex as $name => $value) {
58
                $contract = strstr($name, 'app/') ?: strstr($name, 'app\\');
0 ignored issues
show
It seems like $name can also be of type null and true; however, parameter $haystack of strstr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
                $contract = strstr(/** @scrutinizer ignore-type */ $name, 'app/') ?: strstr($name, 'app\\');
Loading history...
59
                $contract = rtrim($contract, '.php');
60
61
                $contractName = str_replace('/', '\\', ucfirst($contract));
62
63
                //replace only first occurrence
64
                $pos = strpos($contractName, $interfaces);
65
                $repositoryClass = '';
66
                if ($pos !== false) {
67
                    $repositoryClass = substr_replace($contractName, $repositories, $pos, strlen($interfaces));
68
                }
69
70
                //replace only last occurrence
71
                $pos = strrpos($repositoryClass, $interface);
0 ignored issues
show
It seems like $repositoryClass can also be of type array; however, parameter $haystack of strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
                $pos = strrpos(/** @scrutinizer ignore-type */ $repositoryClass, $interface);
Loading history...
72
                if ($pos !== false) {
73
                    $repositoryClass = substr_replace($repositoryClass, $repository, $pos, strlen($interface));
74
                }
75
76
                $this->providers[] = $contractName;
77
                $this->bindings[$contractName] = $repositoryClass;
78
79
                if (
80
                    interface_exists($contractName) &&
81
                    in_array(ContractInterface::class, class_implements($contractName))
82
                ) {
83
                    $this->providers[] = $contractName;
84
                    $this->bindings[$contractName] = $repositoryClass;
85
                }
86
            }
87
        }
88
    }
89
90
    /**
91
     * Bootstrap services.
92
     */
93
    public function boot()
94
    {
95
    }
96
97
    /**
98
     * Register services.
99
     */
100
    public function register()
101
    {
102
    }
103
104
    /**
105
     * Get the services provided by the provider.
106
     *
107
     * @return array
108
     */
109
    public function provides(): array
110
    {
111
        return $this->providers;
112
    }
113
}
114