Completed
Push — master ( 3bf8f4...3f2708 )
by Tobias
06:16
created

src/Transifex.php (5 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\PlatformAdapter\Transifex;
13
14
use BabDev\Transifex\Transifex as TransifexClient;
15
use Psr\Http\Message\ResponseInterface;
16
use Translation\Common\Exception\StorageException;
17
use Translation\Common\Model\Message;
18
use Translation\Common\Storage;
19
20
/**
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class Transifex implements Storage
24
{
25
    /**
26
     * @var TransifexClient
27
     */
28
    private $client;
29
30
    /**
31
     * @var array
32
     */
33
    private $domainToProjectId = [];
34
35
    /**
36
     * @param TransifexClient $client
37
     * @param array           $domainToProjectId
38
     */
39
    public function __construct(TransifexClient $client, array $domainToProjectId)
40
    {
41
        $this->client = $client;
42
        $this->domainToProjectId = $domainToProjectId;
43
    }
44
45
    public function get($locale, $domain, $key)
46
    {
47
        $projectKey = $this->getApiKey($domain);
48
49
        $translation = (string) $this->client->get('translations')->getTranslation($projectKey, $key, $locale)->getBody();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class BabDev\Transifex\TransifexObject as the method getTranslation() does only exist in the following sub-classes of BabDev\Transifex\TransifexObject: BabDev\Transifex\Translations. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
50
        $meta = [];
51
52
        return new Message($key, $domain, $locale, $translation, $meta);
53
    }
54
55
    public function create(Message $message)
56
    {
57
        $projectKey = $this->getApiKey($message->getDomain());
58
59
        $this->client->get('resources')->createResource($projectKey, $message->getKey(), $message->getKey(), 'TXT');
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class BabDev\Transifex\TransifexObject as the method createResource() does only exist in the following sub-classes of BabDev\Transifex\TransifexObject: BabDev\Transifex\Resources. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
60
        $translation = (string) $this->client->get('translations')->getTranslation($projectKey, $message->getKey(), $message->getLocale())->getBody();
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class BabDev\Transifex\TransifexObject as the method getTranslation() does only exist in the following sub-classes of BabDev\Transifex\TransifexObject: BabDev\Transifex\Translations. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
61
        if (empty($translation)) {
62
            $this->client->get('translations')
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class BabDev\Transifex\TransifexObject as the method updateTranslation() does only exist in the following sub-classes of BabDev\Transifex\TransifexObject: BabDev\Transifex\Translations. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
63
                ->updateTranslation(
64
                    $projectKey,
65
                    $message->getKey(),
66
                    $message->getLocale(),
67
                    $message->getTranslation()
68
                );
69
        }
70
    }
71
72
    public function update(Message $message)
73
    {
74
        $projectKey = $this->getApiKey($message->getDomain());
75
76
        /** @var ResponseInterface $response */
77
        $response = $this->client->get('translations')
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class BabDev\Transifex\TransifexObject as the method updateTranslation() does only exist in the following sub-classes of BabDev\Transifex\TransifexObject: BabDev\Transifex\Translations. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
78
            ->updateTranslation($projectKey, $message->getKey(), $message->getLocale(), $message->getTranslation());
79
        // Check it it was any error
80
        if ($response->getStatusCode() !== 200) {
81
            $this->create($message);
82
        }
83
    }
84
85
    public function delete($locale, $domain, $key)
86
    {
87
        // Transifex API does not support deleting translations.
88
    }
89
90
    /**
91
     * @param string $domain
92
     *
93
     * @return string
94
     */
95
    protected function getApiKey($domain)
96
    {
97
        if (isset($this->domainToProjectId[$domain])) {
98
            return $this->domainToProjectId[$domain];
99
        }
100
101
        throw new StorageException(sprintf('Api key for domain "%s" has not been configured.', $domain));
102
    }
103
}
104