BaseCrypt   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clearErrors() 0 7 2
A getErrorMessage() 0 9 3
A throwException() 0 4 1
A openSslErrorString() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Crypt;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Crypt\Exceptions\CryptException;
22
use function openssl_error_string;
23
24
/**
25
 * @package Limoncello\Crypt
26
 */
27
abstract class BaseCrypt
28
{
29
    /**
30
     * @return void
31
     */
32 12
    protected function clearErrors(): void
33
    {
34
        /** @noinspection PhpStatementHasEmptyBodyInspection */
35 12
        while ($this->openSslErrorString() !== false) {
36
            // drop all accumulated error messages if any
37
        }
38
    }
39
40
    /**
41
     * @return null|string
42
     */
43 4
    protected function getErrorMessage(): ?string
44
    {
45 4
        $message = null;
46 4
        while (($errorLine = $this->openSslErrorString()) !== false) {
47 1
            $message .= ($message === null ? $errorLine : PHP_EOL . $errorLine);
48
        }
49
50 4
        return $message;
51
    }
52
53
    /**
54
     * @param CryptException $exception
55
     *
56
     * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
57
     */
58 4
    protected function throwException(CryptException $exception): void
59
    {
60 4
        throw $exception;
61
    }
62
63
    /**
64
     * We need this wrapper for testing purposes so we can mock system call to Open SSL.
65
     *
66
     * @return string|false
67
     */
68 11
    protected function openSslErrorString()
69
    {
70 11
        return openssl_error_string();
71
    }
72
}
73