Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for different types in visibility changes #369

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Formatters/OneLineBetweenClassVisibilityChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function enterNode(Node $node)
return null;
}

// Ignore nodes with exactly the same visibility
if ($this->previousNode->flags === $node->flags) {
// If the two nodes are the same type and have the same visibility skip
if ($node::class === $this->previousNode::class && $this->previousNode->flags === $node->flags) {
$this->previousNode = $node;

return null;
Expand Down Expand Up @@ -104,6 +104,8 @@ public function enterNode(Node $node)
}
}

$this->previousNode = $node;

$node->setAttribute('comments', [new Comment(''), ...$node->getComments()]);

return $node;
Expand Down
4 changes: 2 additions & 2 deletions src/Linters/OneLineBetweenClassVisibilityChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function lint(Parser $parser)
continue;
}

// Ignore nodes with exactly the same visibility
if ($previousNode->flags === $node->flags) {
// If the two nodes are the same type and have the same visibility skip
if ($previousNode::class === $node::class && $previousNode->flags === $node->flags) {
$previousNode = $node;

continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public function catches_missing_line_between_visibility_changes()
class Thing
{
protected const OK = 1;
protected const DOKEY = 1;
protected $bar;
private $ok;
private $foo;
}
file;

Expand All @@ -31,8 +34,12 @@ class Thing
class Thing
{
protected const OK = 1;
protected const DOKEY = 1;

protected $bar;

private $ok;
private $foo;
}
file;

Expand All @@ -56,6 +63,7 @@ class Thing
* The description of something.
*/
private $ok;
private $dokey;
}
file;

Expand All @@ -72,6 +80,7 @@ class Thing
* The description of something.
*/
private $ok;
private $dokey;
}
file;

Expand All @@ -96,6 +105,7 @@ class Thing
* The description of something.
*/
private $ok;
private $dokey;
}
file;

Expand All @@ -117,6 +127,7 @@ class Thing
protected const OK = 1;
// Note to self
private $ok;
private $dokey;
}
file;

Expand All @@ -131,6 +142,7 @@ class Thing

// Note to self
private $ok;
private $dokey;
}
file;

Expand All @@ -153,6 +165,7 @@ class Thing
// Note to self
// Another
private $ok;
private $dokey;
}
file;

Expand All @@ -168,6 +181,7 @@ class Thing
// Note to self
// Another
private $ok;
private $dokey;
}
file;

Expand All @@ -192,6 +206,7 @@ class Thing
// And another
// And another one
private $ok;
private $dokey;
}
file;

Expand All @@ -209,6 +224,7 @@ class Thing
// And another
// And another one
private $ok;
private $dokey;
}
file;

Expand All @@ -231,6 +247,7 @@ class Thing

// TODO
private $ok;
private $dokey;
}
file;

Expand All @@ -253,6 +270,7 @@ class Thing
// public const NOT_OK = 2;

private $ok;
private $dokey;
}
file;

Expand Down Expand Up @@ -281,6 +299,7 @@ class Thing
// Hi there
//
private $ok;
private $dokey;
}
file;

Expand All @@ -307,6 +326,7 @@ public function getThing(): NodeVisitorAbstract
{
protected const OK = 1;
private $ok;
private $dokey;
};
}
}
Expand All @@ -328,6 +348,7 @@ public function getThing(): NodeVisitorAbstract
protected const OK = 1;

private $ok;
private $dokey;
};
}
}
Expand All @@ -337,4 +358,42 @@ public function getThing(): NodeVisitorAbstract

$this->assertSame($expected, $formatted);
}

/** @test */
public function catches_missing_line_between_types()
{
$file = <<<'file'
<?php

namespace App;

class Thing
{
public const FOO = 'bar';
public $bar = 'baz';
protected $foo = 'bar';
protected $bar = 'baz';
}
file;

$expected = <<<'file'
<?php

namespace App;

class Thing
{
public const FOO = 'bar';

public $bar = 'baz';

protected $foo = 'bar';
protected $bar = 'baz';
}
file;

$formatted = (new TFormat)->format(new OneLineBetweenClassVisibilityChanges($file));

$this->assertSame($expected, $formatted);
}
}
34 changes: 34 additions & 0 deletions tests/Linting/Linters/OneLineBetweenClassVisibilityChangesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Thing
{
protected const OK = 1;
private $ok;
private $dokey;
}
file;

Expand All @@ -45,6 +46,7 @@ class Thing
* The description of something.
*/
private $ok;
private $dokey;
}
file;

Expand All @@ -71,6 +73,7 @@ class Thing
* The description of something.
*/
private $ok;
private $dokey;
}
file;

Expand All @@ -94,6 +97,7 @@ class Thing
protected const OK = 1;
// Note to self
private $ok;
private $dokey;
}
file;

Expand All @@ -118,6 +122,7 @@ class Thing
// Note to self
// Another
private $ok;
private $dokey;
}
file;

Expand All @@ -144,6 +149,7 @@ class Thing
// And another
// And another one
private $ok;
private $dokey;
}
file;

Expand All @@ -168,6 +174,7 @@ class Thing

// TODO
private $ok;
private $dokey;
}
file;

Expand All @@ -192,6 +199,7 @@ class Thing
// public const NOT_OK = 2;

private $ok;
private $dokey;
}
file;

Expand Down Expand Up @@ -222,6 +230,7 @@ class Thing
// Hi there
//
private $ok;
private $dokey;
}
file;

Expand All @@ -231,4 +240,29 @@ class Thing

$this->assertEmpty($lints);
}

/** @test */
public function catches_missing_line_between_types()
{
$file = <<<'file'
<?php

namespace App;

class Thing
{
protected const OK = 1;
protected $bar;
private $ok;
private $dokey;
}
file;

$lints = (new TLint)->lint(
new OneLineBetweenClassVisibilityChanges($file)
);

$this->assertEquals(8, $lints[0]->getNode()->getLine());
$this->assertEquals(9, $lints[1]->getNode()->getLine());
}
}
Loading