Completed
Pull Request — develop (#615)
by Narcotic
75:39 queued 66:24
created

TranslatorFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 38
ccs 0
cts 14
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createTranslator() 0 4 1
A addResourceFiles() 0 12 3
1
<?php
2
/**
3
 * a way to load dynamically added resources files to Translator
4
 */
5
6
namespace Graviton\I18nBundle\Translator;
7
8
use Graviton\I18nBundle\Service\I18nCacheUtils;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
/**
12
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
13
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
14
 * @link     http://swisscom.ch
15
 */
16
class TranslatorFactory
17
{
18
19
    /**
20
     * factory method, creates a translator
21
     *
22
     * @param TranslatorInterface $translator translator
23
     * @param I18nCacheUtils      $cacheUtils cache utils
24
     *
25
     * @return TranslatorInterface translator
26
     */
27
    public static function createTranslator(TranslatorInterface $translator, I18nCacheUtils $cacheUtils)
28
    {
29
        return self::addResourceFiles($translator, $cacheUtils->getResources([]));
30
    }
31
32
    /**
33
     * as we cannot add resources files directly (the function from where i lifted this is private),
34
     * we copied this here and add it as Translator would (and does)
35
     *
36
     * @param TranslatorInterface $translator translator
37
     * @param array               $resources  added resources
38
     *
39
     * @return TranslatorInterface translator
40
     */
41
    private static function addResourceFiles(TranslatorInterface $translator, array $resources)
42
    {
43
        foreach ($resources as $locale => $files) {
44
            foreach ($files as $key => $file) {
45
                // filename is domain.locale.format
46
                list($domain, $locale, $format) = explode('.', basename($file), 3);
47
                $translator->addResource($format, $file, $locale, $domain);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Translation\TranslatorInterface as the method addResource() does only exist in the following implementations of said interface: Symfony\Bundle\Framework...slatorWithInvalidLocale, Symfony\Bundle\Framework...\Translation\Translator, Symfony\Component\Translation\Translator.

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...
48
            }
49
        }
50
51
        return $translator;
52
    }
53
}
54