Issues (238)

src/Commands/FileLoaderCommand.php (12 issues)

1
<?php
2
3
namespace Translation\Commands;
4
5
use Illuminate\Console\Command;
0 ignored issues
show
The type Illuminate\Console\Command was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Filesystem\Filesystem;
7
use Translation\Repositories\LanguageRepository;
8
use Translation\Repositories\TranslationRepository;
9
10
class FileLoaderCommand extends Command
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'translator:load';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = "Load language files into the database.";
25
26
    /**
27
     *  Create a new mixed loader instance.
28
     *
29
     * @param \Waavi\Lang\Providers\LanguageProvider      $languageRepository
0 ignored issues
show
The type Waavi\Lang\Providers\LanguageProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     * @param \Waavi\Lang\Providers\LanguageEntryProvider $translationRepository
0 ignored issues
show
The type Waavi\Lang\Providers\LanguageEntryProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
     * @param \Illuminate\Foundation\Application          $app
0 ignored issues
show
The type Illuminate\Foundation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     */
33
    public function __construct(LanguageRepository $languageRepository, TranslationRepository $translationRepository, Filesystem $files, $translationsPath, $defaultLocale)
34
    {
35
        parent::__construct();
36
        $this->languageRepository    = $languageRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property languageRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37
        $this->translationRepository = $translationRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property translationRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
38
        $this->path                  = $translationsPath;
0 ignored issues
show
Bug Best Practice introduced by
The property path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
        $this->files                 = $files;
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->defaultLocale         = $defaultLocale;
0 ignored issues
show
Bug Best Practice introduced by
The property defaultLocale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
    }
42
43
    public function handle()
44
    {
45
        return $this->fire();
0 ignored issues
show
Are you sure the usage of $this->fire() targeting Translation\Commands\FileLoaderCommand::fire() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46
    }
47
48
    /**
49
     *  Execute the console command.
50
     *
51
     * @return void
52
     */
53
    public function fire()
54
    {
55
        $this->loadLocaleDirectories($this->path);
56
    }
57
58
    /**
59
     *  Loads all locale directories in the given path (/en, /es, /fr) as long as the locale corresponds to a language in the database.
60
     *  If a vendor directory is found not inside another vendor directory, the files within it will be loaded with the corresponding namespace.
61
     *
62
     * @param  string $path      Full path to the root directory of the locale directories. Usually /path/to/laravel/resources/lang
63
     * @param  string $namespace Namespace where the language files should be inserted.
64
     * @return void
65
     */
66
    public function loadLocaleDirectories($path, $namespace = '*')
67
    {
68
        $availableLocales = $this->languageRepository->availableLocales();
69
        $directories      = $this->files->directories($path);
70
        foreach ($directories as $directory) {
71
            $locale = basename($directory);
72
            if (in_array($locale, $availableLocales)) {
73
                $this->loadDirectory($directory, $locale, $namespace);
74
            }
75
            if ($locale === 'vendor' && $namespace === '*') {
76
                $this->loadVendor($directory);
77
            }
78
        }
79
    }
80
81
    /**
82
     *  Load all vendor overriden localization packages. Calls loadLocaleDirectories with the appropriate namespace.
83
     *
84
     * @param  string $path Path to vendor locale root, usually /path/to/laravel/resources/lang/vendor.
85
     * @see    http://laravel.com/docs/5.1/localization#overriding-vendor-language-files
86
     * @return void
87
     */
88
    public function loadVendor($path)
89
    {
90
        $directories = $this->files->directories($path);
91
        foreach ($directories as $directory) {
92
            $namespace = basename($directory);
93
            $this->loadLocaleDirectories($directory, $namespace);
94
        }
95
    }
96
97
    /**
98
     *  Load all files inside a locale directory and its subdirectories.
99
     *
100
     * @param  string $path      Path to locale root. Ex: /path/to/laravel/resources/lang/en
101
     * @param  string $locale    Locale to apply when loading the localization files.
102
     * @param  string $namespace Namespace to apply when loading the localization files ('*' by default, or the vendor package name if not)
103
     * @param  string $group     When loading from a subdirectory, the subdirectory's name must be prepended. For example: trans('subdir/file.entry').
104
     * @return void
105
     */
106
    public function loadDirectory($path, $locale, $namespace = '*', $group = '')
107
    {
108
        // Load all files inside subdirectories:
109
        $directories = $this->files->directories($path);
110
        foreach ($directories as $directory) {
111
            $directoryName = str_replace($path . '/', '', $directory);
0 ignored issues
show
The assignment to $directoryName is dead and can be removed.
Loading history...
112
            $dirGroup      = $group . basename($directory) . '/';
113
            $this->loadDirectory($directory, $locale, $namespace, $dirGroup);
114
        }
115
116
        // Load all files in root:
117
        $files = $this->files->files($path);
118
        foreach ($files as $file) {
119
            $this->loadFile($file, $locale, $namespace, $group);
120
        }
121
    }
122
123
    /**
124
     *  Loads the given file into the database
125
     *
126
     * @param  string $path      Full path to the localization file. For example: /path/to/laravel/resources/lang/en/auth.php
127
     * @param  string $locale
128
     * @param  string $namespace
129
     * @param  string $group     Relative from the locale directory's root. For example subdirectory/subdir2/
130
     * @return void
131
     */
132
    public function loadFile($file, $locale, $namespace = '*', $group = '')
133
    {
134
        $group        = $group . basename($file, '.php');
135
        $translations = $this->files->getRequire($file);
136
        $this->translationRepository->loadArray($translations, $locale, $group, $namespace, $locale == $this->defaultLocale);
0 ignored issues
show
The call to Translation\Repositories...Repository::loadArray() has too many arguments starting with $locale == $this->defaultLocale. ( Ignorable by Annotation )

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

136
        $this->translationRepository->/** @scrutinizer ignore-call */ 
137
                                      loadArray($translations, $locale, $group, $namespace, $locale == $this->defaultLocale);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
137
    }
138
}
139