FileTranslationRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 11
c 6
b 0
f 1
lcom 1
cbo 1
dl 0
loc 71
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 19 4
A __construct() 0 5 1
B getTranslationFilenamesFromPaths() 0 23 6
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
                $glob = $this->finder->glob("{$path}/{$locale}/*.php");
62
63
                if ($glob) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $glob of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
64
                    foreach ($glob as $file) {
65
                        $category = str_replace(["$path/", ".php", "{$locale}/"], "", $file);
66
                        $category = str_replace("/", ".", $category);
67
                        $category = !is_int($hint) ? "{$hint}::{$category}" : $category;
68
69
                        $files[$locale][$category] = $file;
70
                    }
71
                }
72
            }
73
        }
74
75
        return $files;
76
    }
77
}
78