Skip to content

Commit a28d4e1

Browse files
committed
Fixed some more SonarQube issues
1 parent f00667a commit a28d4e1

File tree

4 files changed

+50
-42
lines changed

4 files changed

+50
-42
lines changed

src/Robtimus/Obfuscation/Obfuscate.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
final class Obfuscate
1414
{
15-
private static ?Obfuscator $_NONE = null;
15+
private static ?Obfuscator $_none = null;
1616

1717
private function __construct()
1818
{
@@ -55,11 +55,11 @@ public function obfuscateText(string $text): string
5555
*/
5656
public static function none(): Obfuscator
5757
{
58-
if (is_null(Obfuscate::$_NONE)) {
59-
Obfuscate::$_NONE = new class extends Obfuscator
58+
if (is_null(Obfuscate::$_none)) {
59+
Obfuscate::$_none = new class extends Obfuscator
6060
{
6161
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
62-
function __construct()
62+
public function __construct()
6363
{
6464
}
6565

@@ -70,7 +70,7 @@ public function obfuscateText(string $text): string
7070
}
7171
};
7272
}
73-
return Obfuscate::$_NONE;
73+
return Obfuscate::$_none;
7474
}
7575

7676
/**

src/Robtimus/Obfuscation/PortionObfuscatorBuilder.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,14 @@ public function build(): Obfuscator
184184
private string $_mask;
185185

186186
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
187-
public function __construct(int $keepAtStart, int $keepAtEnd, int $atLeastFromStart, int $atLeastFromEnd, int $fixedTotalLength, string $mask)
188-
{
187+
public function __construct(
188+
int $keepAtStart,
189+
int $keepAtEnd,
190+
int $atLeastFromStart,
191+
int $atLeastFromEnd,
192+
int $fixedTotalLength,
193+
string $mask
194+
) {
189195
$this->_keepAtStart = $keepAtStart;
190196
$this->_keepAtEnd = $keepAtEnd;
191197
$this->_atLeastFromStart = $atLeastFromStart;

src/Robtimus/Obfuscation/PropertyObfuscator.php

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,15 @@ public function obfuscateArrayProperties(array $array): array
256256

257257
private function _obfuscateWithDefault(mixed $value, ?Obfuscator $defaultObfuscator): mixed
258258
{
259+
$result = $value;
259260
if (is_scalar($value) || is_null($value)) {
260-
return $this->_obfuscateScalar($value, $defaultObfuscator);
261+
$result = $this->_obfuscateScalar($value, $defaultObfuscator);
262+
} else if (is_object($value)) {
263+
$result = $this->_obfuscateObject($value, $defaultObfuscator);
264+
} else if (is_array($value)) {
265+
$result = $this->_obfuscateArray($value, $defaultObfuscator);
261266
}
262-
if (is_object($value)) {
263-
return $this->_obfuscateObject($value, $defaultObfuscator);
264-
}
265-
if (is_array($value)) {
266-
return $this->_obfuscateArray($value, $defaultObfuscator);
267-
}
268-
return $value;
267+
return $result;
269268
}
270269

271270
// phpcs:disable Generic.Files.LineLength.TooLong
@@ -280,29 +279,28 @@ private function _obfuscateValue(mixed $value, ?array $propertyConfig, ?Obfuscat
280279
if (is_null($propertyConfig)) {
281280
return $this->_obfuscateWithDefault($value, $defaultObfuscator);
282281
}
282+
$result = $value;
283283
$obfuscator = $propertyConfig['obfuscator'];
284284
if (is_scalar($value) || is_null($value)) {
285-
return $this->_obfuscateScalar($value, $obfuscator);
286-
}
287-
if (is_object($value)) {
285+
$result = $this->_obfuscateScalar($value, $obfuscator);
286+
} else if (is_object($value)) {
288287
$obfuscationMode = $propertyConfig['forObjects'];
289-
return match ($obfuscationMode) {
288+
$result = match ($obfuscationMode) {
290289
PropertyObfuscationMode::SKIP => $value,
291290
PropertyObfuscationMode::EXCLUDE => $this->_obfuscateWithDefault($value, $defaultObfuscator),
292291
PropertyObfuscationMode::INHERIT => $this->_obfuscateScalars($value, $obfuscator),
293292
PropertyObfuscationMode::INHERIT_OVERRIDABLE => $this->_obfuscateWithDefault($value, $obfuscator),
294293
};
295-
}
296-
if (is_array($value)) {
294+
} else if (is_array($value)) {
297295
$obfuscationMode = $propertyConfig['forArrays'];
298-
return match ($obfuscationMode) {
296+
$result = match ($obfuscationMode) {
299297
PropertyObfuscationMode::SKIP => $value,
300298
PropertyObfuscationMode::EXCLUDE => $this->_obfuscateWithDefault($value, $defaultObfuscator),
301299
PropertyObfuscationMode::INHERIT => $this->_obfuscateScalars($value, $obfuscator),
302300
PropertyObfuscationMode::INHERIT_OVERRIDABLE => $this->_obfuscateWithDefault($value, $obfuscator),
303301
};
304302
}
305-
return $value;
303+
return $result;
306304
}
307305

308306
private function _obfuscateObject(object $object, ?Obfuscator $defaultObfuscator): stdClass
@@ -318,9 +316,9 @@ private function _obfuscateObject(object $object, ?Obfuscator $defaultObfuscator
318316
/**
319317
* Obfuscates an array.
320318
*
321-
* @param array<mixed> $array
319+
* @param array<int|string, mixed> $array
322320
*
323-
* @return array<mixed>
321+
* @return array<int|string, mixed>
324322
*/
325323
private function _obfuscateArray(array $array, ?Obfuscator $defaultObfuscator): array
326324
{
@@ -338,24 +336,23 @@ private function _obfuscateArray(array $array, ?Obfuscator $defaultObfuscator):
338336

339337
private function _obfuscateScalars(mixed $value, Obfuscator $obfuscator): mixed
340338
{
339+
$result = $value;
341340
if (is_scalar($value) || is_null($value)) {
342-
return $this->_obfuscateScalar($value, $obfuscator);
343-
}
344-
if (is_object($value)) {
341+
$result = $this->_obfuscateScalar($value, $obfuscator);
342+
} else if (is_object($value)) {
345343
$obfuscated = new stdClass();
346344
foreach ($value as $propertyName => $propertyValue) {
347345
$obfuscated->$propertyName = $this->_obfuscateScalars($propertyValue, $obfuscator);
348346
}
349-
return $obfuscated;
350-
}
351-
if (is_array($value)) {
347+
$result = $obfuscated;
348+
} else if (is_array($value)) {
352349
$obfuscated = [];
353350
foreach ($value as $propertyName => $propertyValue) {
354351
$obfuscated[$propertyName] = $this->_obfuscateScalars($propertyValue, $obfuscator);
355352
}
356-
return $obfuscated;
353+
$result = $obfuscated;
357354
}
358-
return $value;
355+
return $result;
359356
}
360357

361358
/**

src/Robtimus/Obfuscation/SplitPoint.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,8 @@ public function obfuscateText(string $text): string
142142
*/
143143
public static function atFirst(string $s): SplitPoint
144144
{
145-
if (mb_strlen($s) === 0) {
146-
throw new ValueError("cannot split on empty strings");
147-
}
145+
SplitPoint::_checkNotEmpty($s);
146+
148147
return new class($s) extends SplitPoint
149148
{
150149
private string $_splitAt;
@@ -183,9 +182,8 @@ public function splitLength(): int
183182
*/
184183
public static function atLast(string $s): SplitPoint
185184
{
186-
if (mb_strlen($s) === 0) {
187-
throw new ValueError("cannot split on empty strings");
188-
}
185+
SplitPoint::_checkNotEmpty($s);
186+
189187
return new class($s) extends SplitPoint
190188
{
191189
private string $_splitAt;
@@ -225,12 +223,12 @@ public function splitLength(): int
225223
*/
226224
public static function atNth(string $s, int $occurrence): SplitPoint
227225
{
228-
if (mb_strlen($s) === 0) {
229-
throw new ValueError("cannot split on empty strings");
230-
}
226+
SplitPoint::_checkNotEmpty($s);
227+
231228
if ($occurrence < 0) {
232229
throw new ValueError("$occurrence < 0");
233230
}
231+
234232
return new class($s, $occurrence) extends SplitPoint
235233
{
236234
private string $_splitAt;
@@ -262,4 +260,11 @@ public function splitLength(): int
262260
}
263261
};
264262
}
263+
264+
private static function _checkNotEmpty(string $s): void
265+
{
266+
if (mb_strlen($s) === 0) {
267+
throw new ValueError("cannot split on empty strings");
268+
}
269+
}
265270
}

0 commit comments

Comments
 (0)