Skip to content

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkan committed Jul 25, 2024
1 parent b695eff commit 387dfe6
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 32 deletions.
7 changes: 4 additions & 3 deletions src/CodeHelper/ArrayCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ public function getSourceArray(): array
foreach ($formattedValue as $z) {
$rows[] = $z;
}
$rows[] = $lastValue .',';
$rows[] = $lastValue . ',';
continue;
}

$formattedValue = FormatValue::format($value);
if ($value instanceof CodeInterface &&
if (
$value instanceof CodeInterface &&
is_array($formattedValue) &&
count($formattedValue) === 1 &&
is_string($formattedValue[0])
Expand Down Expand Up @@ -85,7 +86,7 @@ public function getSourceArray(): array
}
elseif (is_string($formattedValue)) {
$rows[] = sprintf(
"%s,",
'%s,',
$formattedValue
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/CodeHelper/CastCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
public function getSourceArray(): array
{
return [
'(' . $this->castType .')' . $this->reference->toString(),
'(' . $this->castType . ')' . $this->reference->toString(),
];
}
}
2 changes: 1 addition & 1 deletion src/CodeHelper/ClassMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function this(string $method, array $params = []): self
public function __construct(
private VariableReference $class,
private string $method,
private array $params = []
private array $params = [],
) {
$this->identifier = $class->toString();
}
Expand Down
1 change: 0 additions & 1 deletion src/CodeHelper/MethodCallInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

interface MethodCallInterface
{

}
2 changes: 1 addition & 1 deletion src/CodeHelper/StaticMethodCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class StaticMethodCall implements CodeInterface, MethodCallInterface
public function __construct(
protected Identifier|string $class,
protected string $method,
protected array $params = []
protected array $params = [],
) {
$this->identifier = Identifier::fromUnknown($class)->getName();
$this->callIdentifier = '::';
Expand Down
4 changes: 2 additions & 2 deletions src/PhpDocElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Fredrik Wallgren <[email protected]>
* @author Andreas Sundqvist <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class PhpDocElement
{
Expand All @@ -19,7 +19,7 @@ public function __construct(
private string $type,
string|Type $dataType,
private string $variableName,
private string $description
private string $description,
) {
$this->datatype = is_string($dataType) ? Type::fromString($dataType) : $dataType;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
array $params,
protected array|CodeInterface $body,
protected ?Type $returnTypeHint = null,
protected ?PhpDocComment $comment = null
protected ?PhpDocComment $comment = null,
) {
$this->identifier = Identifier::fromUnknown($identifier);
foreach ($params as $param) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function addVariable(PhpVariable $variable, bool $forcePublic = false): s
return parent::addVariable($variable);
}

public static function fromClass(Identifier $identifier, PhpClass $class): self
public static function fromClass(Identifier $identifier, PhpClass $class): self
{
$interface = new self($identifier);
$interface->uses = $class->uses;
Expand Down
3 changes: 2 additions & 1 deletion src/PhpMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ public function setParent(PhpTrait|PhpInterface|PhpClass $parent): static
if ($this->constructorAutoAssign) {
foreach ($this->params as $param) {
$var = $param->getVariable();
if ($var &&
if (
$var &&
!$parent instanceof PhpInterface &&
!$parent->hasVariable($var->getIdentifier())
) {
Expand Down
3 changes: 2 additions & 1 deletion src/PhpParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function getParent(): PhpFunction|PhpMethod|null
public function setParent(PhpFunction|PhpMethod $parent): void
{
$this->parent = $parent;
if ($this->variable &&
if (
$this->variable &&
$parent instanceof PhpMethod &&
$parent->doConstructorAutoAssign()
) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpStan/ExtendsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ExtendsField extends PhpDocElement implements HasIdentifiers

public function __construct(
private Identifier $extends,
TemplateField|Identifier ...$templateFields
TemplateField|Identifier ...$templateFields,
) {
parent::__construct('extends', Type::empty(), '', '');
$this->templateFields = $templateFields;
Expand Down
2 changes: 1 addition & 1 deletion src/PhpStan/GenericSourceTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Stefna\PhpCodeBuilder\PhpStan;

Expand Down
2 changes: 1 addition & 1 deletion src/PhpStan/ImplementsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class ImplementsField extends PhpDocElement implements HasIdentifiers

public function __construct(
private Identifier $implement,
TemplateField|Identifier ...$templateFields
TemplateField|Identifier ...$templateFields,
) {
parent::__construct('implements', Type::empty(), '', '');
$this->templateFields = $templateFields;
Expand Down
3 changes: 2 additions & 1 deletion src/Renderer/Php74Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function renderVariable(PhpVariable $variable, ?PhpTrait $parent = null):
Type::setInvalidReturnTypes($this->invalidReturnTypes);
$ret = [];

if ($variable->getType()->isNullable() &&
if (
$variable->getType()->isNullable() &&
$variable->getInitializedValue() === PhpVariable::NO_VALUE &&
$variable->getType()->getTypeHint() &&
$variable->getType()->getType() !== 'mixed'
Expand Down
10 changes: 6 additions & 4 deletions src/Renderer/Php8Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function renderParams(PhpFunction $function, PhpParam ...$params): array|
{
$multiLine = false;
$includeVariableAttributes = false;
if ($function instanceof PhpMethod &&
if (
$function instanceof PhpMethod &&
$function->isConstructor() &&
$function->doConstructorAutoAssign()
) {
Expand Down Expand Up @@ -205,7 +206,8 @@ public function renderMethod(PhpMethod $method): array
$ret = $this->renderFunction($method);

if ($method->isConstructor()) {
if (is_array($ret[array_key_last($ret) - 1]) &&
if (
is_array($ret[array_key_last($ret) - 1]) &&
count($ret[array_key_last($ret) - 1]) === 0
) {
unset($ret[array_key_last($ret) - 1]);
Expand Down Expand Up @@ -249,7 +251,7 @@ protected function renderPromotedPropertyModifiers(
public function renderAttribute(PhpAttribute $attr): array
{
$args = $attr->getArgs();
$start = '#['.$attr->getIdentifier()->toString();
$start = '#[' . $attr->getIdentifier()->toString();
if (!$args) {
return [
$start . ']',
Expand All @@ -262,7 +264,7 @@ public function renderAttribute(PhpAttribute $attr): array
$argString = implode(',' . PHP_EOL, $args);
}
return [
$start.'(' . $argString . ')]',
$start . '(' . $argString . ')]',
];
}

Expand Down
12 changes: 6 additions & 6 deletions tests/CodeHelper/ArrayCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testSimpleAssocArray(): void
{
$array = new ArrayCode([
'test1' => 2,
'test2' => "string",
'test2' => 'string',
'test3' => true,
]);

Expand All @@ -30,7 +30,7 @@ public function testCustomIndentLevel(): void
{
$array = new ArrayCode([
'test1' => 2,
'test2' => "string",
'test2' => 'string',
'test3' => true,
]);

Expand All @@ -49,7 +49,7 @@ public function testNestedAssocArray(): void
{
$array = new ArrayCode([
'test1' => 2,
'test2' => "string",
'test2' => 'string',
'test3' => true,
'test4' => [
'sub1' => 'test',
Expand Down Expand Up @@ -117,9 +117,9 @@ public function testListOfVariable(): void
$var,
]);

$this->assertSame("[
\$this->testVar,
]", trim(FlattenSource::source($array->getSourceArray())));
$this->assertSame('[
$this->testVar,
]', trim(FlattenSource::source($array->getSourceArray())));
}

public function testListOfTypes(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/CodeHelper/InstantiateClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function testMixedParamsInInstantiateClass(): void
$this->assertSame([
'new self(',
[
"DateTimeImmutable::class,",
"EnumCase::class,",
'DateTimeImmutable::class,',
'EnumCase::class,',
'[',
[
"'id',",
Expand Down
1 change: 0 additions & 1 deletion tests/CodeHelper/Methods/ComplexTypeWithJsonSerialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

final class ComplexTypeWithJsonSerialize implements \JsonSerializable
{

public function jsonSerialize(): string
{
return 'complexType';
Expand Down
2 changes: 1 addition & 1 deletion tests/CodeHelper/ReturnCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testArray(): void
{
$array = new ReturnCode(new ArrayCode([
'test1' => 2,
'test2' => "string",
'test2' => 'string',
'test3' => true,
]));

Expand Down
2 changes: 1 addition & 1 deletion tests/CodeHelper/VariableReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public function testArrayVariableWithDynamicKey(): void
$key = new VariableReference('key');
$var = VariableReference::array('input', $key);

$this->assertSame("\$input[\$key]", $var->toString());
$this->assertSame('$input[$key]', $var->toString());
}
}

0 comments on commit 387dfe6

Please sign in to comment.