Skip to content

Commit

Permalink
Improve the way to retrieve the properties name
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Jun 17, 2022
1 parent bf8a3b2 commit 25a9dae
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/Codor/Sniffs/Classes/PropertyDeclarationSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,25 @@ protected function isVisibilityToken($token): bool
*/
protected function handleVisibilityToken($tokens, $index)
{
$possibleVariable = $tokens[$index + 2];
if ($possibleVariable['type'] !== 'T_VARIABLE') {
$count = count($tokens);

for ($i = $index + 1; $i < $count; $i++) {
$possibleVariable = $tokens[$i];

if ($possibleVariable['type'] === 'T_CONST'
|| $possibleVariable['type'] === 'T_FUNCTION'
|| $possibleVariable['type'] === 'T_SEMICOLON'
) {
return;
}

if ($possibleVariable['type'] !== 'T_VARIABLE') {
continue;
}

$this->memberVars[] = str_replace('$', '', $possibleVariable['content']);
return;
}
$this->memberVars[] = str_replace('$', '', $possibleVariable['content']);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class Foobar
{
public array $baz;

public function __construct(array $baz)
{
$this->baz = $baz
}
}
8 changes: 8 additions & 0 deletions tests/Sniffs/Classes/PropertyDeclarationSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ public function it_produces_a_warning_when_a_class_has_one_undeclared_property_b
$this->assertCount(1, $warningMessages);
$this->assertAllEqual('Class contains undeclared property baz.', $warningMessages);
}

/** @test */
public function it_produces_no_errors_when_a_class_has_one_typed_property()
{
$results = $this->runner->sniff('ClassWithOneDeclaredTypedProperty.inc');
$this->assertSame(0, $results->getErrorCount());
$this->assertSame(0, $results->getWarningCount());
}
}

0 comments on commit 25a9dae

Please sign in to comment.