Skip to content

Commit

Permalink
Fix code style and phpstan
Browse files Browse the repository at this point in the history
Also refactored some code to make it easier to read
  • Loading branch information
sunkan committed Apr 4, 2023
1 parent fe07a16 commit b7b55f7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 16 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ parameters:
count: 1
path: src/CodeHelper/TernaryOperator.php

-
message: "#^Match expression does not handle remaining value\\: string$#"
count: 1
path: src/FormatValue.php

-
message: "#^Property Stefna\\\\PhpCodeBuilder\\\\PhpStan\\\\ExtendsField\\:\\:\\$templateFields \\(array\\<int, Stefna\\\\PhpCodeBuilder\\\\PhpStan\\\\TemplateField\\|Stefna\\\\PhpCodeBuilder\\\\ValueObject\\\\Identifier\\>\\) does not accept array\\<int\\|string, Stefna\\\\PhpCodeBuilder\\\\PhpStan\\\\TemplateField\\|Stefna\\\\PhpCodeBuilder\\\\ValueObject\\\\Identifier\\>\\.$#"
count: 1
Expand Down
5 changes: 4 additions & 1 deletion src/CodeHelper/ForeachCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

final class ForeachCode implements CodeInterface
{
/** @var callable(string $keyName, string $valueName): mixed[] */
/** @var callable(VariableReference $keyName, VariableReference $valueName): mixed[] */
private $loopSourceCallback;

/**
* @param callable(VariableReference $keyName, VariableReference $valueName): mixed[] $loopSourceCallback
*/
public function __construct(
private VariableReference $reference,
callable $loopSourceCallback,
Expand Down
5 changes: 4 additions & 1 deletion src/CodeHelper/JsonSerializeTypeCallResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function resolve(Type $type, VariableReference $variableReference): TypeC
new ForeachCode($variableReference, function (
VariableReference $keyName,
VariableReference $valueName,
) use ($arrayType, $localVariable) {
) use (
$arrayType,
$localVariable,
) {
[$arrayCall, $arrayExtraCode] = $this->classTypeResolver($valueName, $arrayType);
/** @var string $arrayCallStr */
$arrayCallStr = $arrayCall->getSourceArray()[0];
Expand Down
16 changes: 12 additions & 4 deletions src/CodeHelper/TernaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class TernaryOperator implements CodeInterface
{
public static function nullableCall(
VariableReference $variableReference,
MethodCallInterface $call,
MethodCallInterface&CodeInterface $call,
): self {
return new self(
$variableReference->toString(),
Expand Down Expand Up @@ -52,7 +52,7 @@ public function getSourceArray(): array
$source[0] .= $this->successCode;
$source[0] .= ' : ';
}
elseif (is_array($this->successCode) && count($this->successCode) === 1 && is_string($this->successCode[0])) {
elseif (count($this->successCode) === 1 && is_string($this->successCode[0])) {
$source[0] .= $this->successCode[0];
$source[0] .= ' : ';
}
Expand All @@ -69,9 +69,17 @@ public function getSourceArray(): array
$source = FlattenSource::applySourceOn($code, $source);
}

if (is_string($this->failureCode) || (is_array($this->failureCode) && count($this->failureCode) === 1)) {
if (is_string($this->failureCode)) {
if (is_string($source[array_key_last($source)])) {
$source[array_key_last($source)] .= is_string($this->failureCode) ? $this->failureCode : $this->failureCode[0];
$source[array_key_last($source)] .= $this->failureCode;
}
else {
$source[] = $this->failureCode;
}
}
elseif (count($this->failureCode) === 1) {
if (is_string($source[array_key_last($source)])) {
$source[array_key_last($source)] .= $this->failureCode[0];
}
else {
$source[] = $this->failureCode;
Expand Down
9 changes: 6 additions & 3 deletions src/Renderer/Php8Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ class Php8Renderer extends Php74Renderer
{
protected function formatTypeHint(?Type $type): ?string
{
if ($type?->isUnion()) {
if (!$type) {
return null;
}

if ($type->isUnion()) {
$typeHint = [];
if ($type->isNullable()) {
$typeHint[] = 'null';
Expand All @@ -33,7 +37,7 @@ protected function formatTypeHint(?Type $type): ?string
return 'mixed';
}

return $type?->getTypeHint();
return $type->getTypeHint();
}

/**
Expand All @@ -45,7 +49,6 @@ public function renderVariable(PhpVariable $variable, ?PhpTrait $parent = null):
return null;
}


$type = $variable->getType();
if ($type->getType() === 'mixed') {
$variable->getComment()?->removeVar();
Expand Down
2 changes: 1 addition & 1 deletion src/ValueObject/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function addUnion(Type|string $type): void
$this->types[] = $type;
}

public function getTypeHint($renderUnion = false): ?string
public function getTypeHint(bool $renderUnion = false): ?string
{
if (count($this->types) > 1) {
if ($renderUnion) {
Expand Down
3 changes: 2 additions & 1 deletion tests/Renderer/PhpMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public function testConstructorPromotionWithUnionType()
$type->addUnion('null');
$type->addUnion(\DateTimeImmutable::class);
$ctor = PhpMethod::constructor([
new PhpParam('test1',
new PhpParam(
'test1',
$type,
autoCreateVariable: true,
),
Expand Down

0 comments on commit b7b55f7

Please sign in to comment.