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) { |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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.