Issues (74)

src/HasEncryption.php (3 issues)

1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
/**
6
 * This trait allow encryption for fields that don't
7
 * require a blind index
8
 */
9
trait HasEncryption
10
{
11
    use HasBaseEncryption;
12
13
    /**
14
     * prepValueForDB gets passed $this->value
15
     *
16
     * @param string $value
17
     * @return string
18
     */
19
    public function prepValueForDB($value)
20
    {
21
        if (!$value) {
22
            if ($this->getNullifyEmpty() || $value === null) {
0 ignored issues
show
It seems like getNullifyEmpty() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
            if ($this->/** @scrutinizer ignore-call */ getNullifyEmpty() || $value === null) {
Loading history...
23
                return null;
24
            }
25
            return '';
26
        }
27
        // Don't encrypt twice
28
        if (EncryptHelper::isEncrypted($value)) {
29
            return $value;
30
        }
31
        $aad = $this->encryptionAad;
32
        $encryptedValue = $this->getEncryptedField()->encryptValue($value, $aad);
33
        return $encryptedValue;
34
    }
35
36
    public function setValue($value, $record = null, $markChanged = true)
0 ignored issues
show
The parameter $markChanged is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
    public function setValue($value, $record = null, /** @scrutinizer ignore-unused */ $markChanged = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        $this->setEncryptionAad($record);
39
40
        // Return early if we keep encrypted value in memory
41
        if (!EncryptHelper::getAutomaticDecryption()) {
42
            $this->value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
            return $this;
44
        }
45
46
        // $markChanged is not used
47
        // The value might come encrypted from the database
48
        if ($value && EncryptHelper::isEncrypted($value)) {
49
            $this->value = $this->decryptValue($value);
50
        } else {
51
            $this->value = $value;
52
        }
53
        return $this;
54
    }
55
}
56