Completed
Push — master ( db47a2...0cf60d )
by Neomerx
03:19
created

BaseCrypt::openSslErrorString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Limoncello\Crypt;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Crypt\Exceptions\CryptException;
20
21
/**
22
 * @package Limoncello\Crypt
23
 */
24
abstract class BaseCrypt
25
{
26
    /**
27
     * @return void
28
     */
29 12
    protected function clearErrors(): void
30
    {
31
        /** @noinspection PhpStatementHasEmptyBodyInspection */
32 12
        while ($this->openSslErrorString() !== false) {
0 ignored issues
show
Unused Code introduced by
This while loop is empty and can be removed.

This check looks for while loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

Consider removing the loop.

Loading history...
33
            // drop all accumulated error messages if any
34
        }
35
    }
36
37
    /**
38
     * @return null|string
39
     */
40 4
    protected function getErrorMessage(): ?string
41
    {
42 4
        $message = null;
43 4
        while (($errorLine = $this->openSslErrorString()) !== false) {
44 1
            $message .= ($message === null ? $errorLine : PHP_EOL . $errorLine);
45
        }
46
47 4
        return $message;
48
    }
49
50
    /**
51
     * @param CryptException $exception
52
     *
53
     * @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...
54
     */
55 4
    protected function throwException(CryptException $exception): void
56
    {
57 4
        throw $exception;
58
    }
59
60
    /**
61
     * We need this wrapper for testing purposes so we can mock system call to Open SSL.
62
     *
63
     * @return string|false
64
     */
65 11
    protected function openSslErrorString()
66
    {
67 11
        return openssl_error_string();
68
    }
69
}
70