Skip to content

Commit 2b14449

Browse files
committed
[refactor] SONAR
1. clean up code smells and improve code structure
1 parent 75cee25 commit 2b14449

11 files changed

Lines changed: 67 additions & 50 deletions

File tree

src/Attributes/Defaults.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class Defaults
2424
/**
2525
* @param mixed $value Fallback value to apply when the property key is absent.
2626
*/
27-
public function __construct(
27+
private function __construct(
2828
public mixed $value
2929
) {}
3030
}

src/Attributes/KeepOnNull.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@
1919
final class KeepOnNull
2020
{
2121
private function __construct()
22-
{}
22+
{
23+
// Prevents manual instantiation of the attribute.
24+
}
2325
}

src/Attributes/Lax.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
final class Lax
1616
{
1717
private function __construct()
18-
{}
18+
{
19+
// Prevents manual instantiation of the attribute.
20+
}
1921
}

src/Attributes/SkipOnNull.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@
1515
final class SkipOnNull
1616
{
1717
private function __construct()
18-
{}
18+
{
19+
// Prevents manual instantiation of the attribute.
20+
}
1921
}

src/Attributes/Spec.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
final class Spec
1919
{
2020
private function __construct()
21-
{}
21+
{
22+
// Prevents manual instantiation of the attribute.
23+
}
2224
}

src/Attributes/Strict.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@
1616
final class Strict
1717
{
1818
private function __construct()
19-
{}
19+
{
20+
// Prevents manual instantiation of the attribute.
21+
}
2022
}

src/Attributes/ValidateFromSelf.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@
1818
final class ValidateFromSelf
1919
{
2020
private function __construct()
21-
{}
21+
{
22+
// Prevents manual instantiation of the attribute.
23+
}
2224
}

src/CLI/Cacher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private function indexDirectory(string $dir): void
119119
$obj = $ref->newInstanceWithoutConstructor(); // NOSONAR
120120
$method->invoke(null, $obj);
121121
self::defaultValueValidate($class, $obj::defaultValues(), $ref->getProperties());
122-
} catch (DefinitionException | Throwable $e) {
122+
} catch (Throwable $e) {
123123
match (true) {
124124
!self::$silent && $e instanceof DefinitionException => fwrite(STDERR, "\033[33m[Skipped] $class: {$e->getMessage()}\033[0m\n"),
125125
default => null

src/CLI/Writer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,11 @@ private static function tryInstantiateClass(string $class): void
330330
$ref = new ReflectionClass($class);
331331
($method = $ref->getMethod('buildPropertyInheritanceChain'))->setAccessible(true); // NOSONAR
332332
$method->invoke(null, $ref->newInstanceWithoutConstructor()); // NOSONAR
333-
} catch (DefinitionException | Throwable $e) {
334-
if ($e instanceof DefinitionException && !self::$silent) {
335-
fwrite(STDERR, "\033[33m[Skipped] $class: {$e->getMessage()}\033[0m\n");
336-
}
337-
// Silently skip classes that cannot be instantiated
333+
} catch (Throwable $e) {
334+
match (true) {
335+
!self::$silent && $e instanceof DefinitionException => fwrite(STDERR, "\033[33m[Skipped] $class: {$e->getMessage()}\033[0m\n"),
336+
default => null
337+
};
338338
}
339339
}
340340

src/CLI/writer/Markdown.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ public static function contentBlocksGenerate(array $classMap, array $entry): arr
8282
/** @var ReflectionProperty $propRef */
8383
$propRef = $type['propertyRef'] ?? $ref->getProperty($type['propertyName']);
8484
$propDocs = self::docParser($propRef->getDocComment());
85-
$required = $type['allowsNull'] ? '' : 'yes';
85+
$required = match ($type['allowsNull']) {
86+
true => '',
87+
default => 'yes'
88+
};
8689
if ($type['isUnion']) {
8790
/** @var UnionType $type */
8891
$typeNames = array_map(self::unionTypeNamesParser(...), $type['types']);
@@ -99,7 +102,10 @@ public static function contentBlocksGenerate(array $classMap, array $entry): arr
99102
self::$enums[$type['typename']['string']] = true;
100103
}
101104
}
102-
$desc = $propDocs['desc'] ?: '-';
105+
$desc = match (true) {
106+
isset($propDocs['desc']) => $propDocs['desc'],
107+
default => '-'
108+
};
103109
$default = '-';
104110
if (isset($type['defaults'])) {
105111
$default = $type['defaults'];

0 commit comments

Comments
 (0)