Completed
Push — master ( c62883...795ab8 )
by Nicolas
04:44
created

getTranslationFilenamesFromPaths()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 11
nc 5
nop 1
1
<?php namespace Modules\Translation\Repositories\File;
2
3
use Illuminate\Filesystem\Filesystem;
4
use Illuminate\Translation\LoaderInterface;
5
use Modules\Translation\Repositories\FileTranslationRepository as FileTranslationRepositoryInterface;
6
7
class FileTranslationRepository implements FileTranslationRepositoryInterface
8
{
9
    /**
10
     * @var Filesystem
11
     */
12
    private $finder;
13
    /**
14
     * @var LoaderInterface
15
     */
16
    private $loader;
17
18
    public function __construct(Filesystem $finder, LoaderInterface $loader)
19
    {
20
        $this->finder = $finder;
21
        $this->loader = $loader;
22
    }
23
24
    /**
25
     * Get all the translations for all modules on disk
26
     * @return array
27
     */
28
    public function all()
29
    {
30
        $files = $this->getTranslationFilenamesFromPaths($this->loader->paths());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Translation\LoaderInterface as the method paths() does only exist in the following implementations of said interface: Modules\Translation\Services\TranslationLoader.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
31
32
        $translations = [];
33
34
        foreach ($files as $locale => $files) {
35
            foreach ($files as $namespace => $file) {
36
                $trans = $this->finder->getRequire($file);
37
                $trans = array_dot($trans);
38
39
                foreach ($trans as $key => $value) {
40
                    $translations[$locale]["{$namespace}.{$key}"] = $value;
41
                }
42
            }
43
        }
44
45
        return $translations;
46
    }
47
48
    /**
49
     * Get all of the names of the Translations files from an array of Paths.
50
     * Returns [ 'translationkeyprefix' => 'filepath' ]
51
     * @param array $paths
52
     * @return array
53
     */
54
    protected function getTranslationFilenamesFromPaths(array $paths)
55
    {
56
        $files   = [];
57
        $locales = config('laravellocalization.supportedLocales');
58
59
        foreach ($paths as $hint => $path) {
60
            foreach ($locales as $locale => $language) {
61
                foreach ($this->finder->glob("{$path}/{$locale}/*.php") as $file) {
62
                    $category = str_replace(["$path/", ".php", "{$locale}/"], "", $file);
63
                    $category = str_replace("/", ".", $category);
64
                    $category = !is_int($hint) ? "{$hint}::{$category}" : $category;
65
66
                    $files[$locale][$category] = $file;
67
                }
68
            }
69
        }
70
71
        return $files;
72
    }
73
}
74