Passed
Push — master ( 725072...b8e92f )
by Francesco
05:19
created

Encryption.php (4 issues)

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 MesCryptoBundle package.
5
 *
6
 * (c) Francesco Cartenì <http://www.multimediaexperiencestudio.it/>
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 Mes\Security\CryptoBundle;
13
14
use Defuse\Crypto\Crypto as BaseCrypto;
15
use Mes\Security\CryptoBundle\Model\Key;
16
use Mes\Security\CryptoBundle\Model\KeyInterface;
17
18
/**
19
 * Class Encryption.
20
 */
21
final class Encryption extends AbstractEncryption
22
{
23
    /**
24
     * {@inheritdoc}
25
     *
26
     * @throw \Defuse\Crypto\Exception\EnvironmentIsBrokenException
27
     */
28 2
    public function encrypt($plaintext, KeyInterface $key)
29
    {
30 2
        return BaseCrypto::encrypt($plaintext, $this->unlockKey($key));
0 ignored issues
show
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\Crypto::encrypt() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     *
36
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
37
     * @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException
38
     */
39 5
    public function decrypt($ciphertext, KeyInterface $key)
40
    {
41 5
        return BaseCrypto::decrypt($ciphertext, $this->unlockKey($key));
0 ignored issues
show
$key of type object<Mes\Security\Cryp...dle\Model\KeyInterface> is not a sub-type of object<Mes\Security\CryptoBundle\Model\Key>. It seems like you assume a concrete implementation of the interface Mes\Security\CryptoBundle\Model\KeyInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
It seems like $this->unlockKey($key) targeting Mes\Security\CryptoBundle\Encryption::unlockKey() can also be of type object<Defuse\Crypto\KeyProtectedByPassword>; however, Defuse\Crypto\Crypto::decrypt() does only seem to accept object<Defuse\Crypto\Key>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
42
    }
43
44
    /**
45
     * @param Key|KeyInterface $key
46
     *
47
     * @return \Defuse\Crypto\Key|\Defuse\Crypto\KeyProtectedByPassword
48
     */
49 7
    private function unlockKey(Key $key)
50
    {
51 7
        return $key->unlock()
52 6
                   ->getRawKey();
53
    }
54
}
55