Skip to content

Commit

Permalink
Skip Livewire property (#33)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
TomasVotruba and actions-user authored Feb 27, 2023
1 parent 049ab5a commit 6fe7f27
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/code_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ jobs:
name: 'Composer Validate'
run: composer validate --ansi

-
name: 'Rector'
run: composer rector --ansi

-
name: 'Coding Standard'
run: composer fix-cs --ansi
Expand Down
47 changes: 47 additions & 0 deletions .github/workflows/rector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Rector

on:
pull_request: null


jobs:
rector:
# Don't run on forks.
if: github.repository == 'tomasvotruba/unused-public'

runs-on: ubuntu-latest

steps:
-
uses: actions/checkout@v3
with:
# Must be used to trigger workflow after push
token: ${{ secrets.ACCESS_TOKEN }}

-
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
coverage: none

- uses: "ramsey/composer-install@v2"

## First run Rector - here can't be --dry-run !!! it would stop the job with it and not commit anything in the future
- run: vendor/bin/rector --ansi

- run: vendor/bin/ecs check --fix --ansi

# see https://github.com/EndBug/add-and-commit
-
# commit only to core contributors who have repository access
if: github.event.pull_request.head.repo.full_name == github.repository
uses: EndBug/[email protected]
with:
# The arguments for the `git add` command (see the paragraph below for more info)
add: .
message: "[ci-review] Rector Rectify"
author_name: "GitHub Action"
author_email: "[email protected]"
env:
# to get push access
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }}
23 changes: 20 additions & 3 deletions src/Collectors/PublicPropertyCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TomasVotruba\UnusedPublic\Collectors;

use Livewire\Component;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PHPStan\Analyser\Scope;
Expand All @@ -18,6 +19,11 @@
*/
final class PublicPropertyCollector implements Collector
{
/**
* @var array<class-string<Component>>
*/
private const CLASSES_TO_SKIP = ['Livewire\Component'];

public function __construct(
private readonly ApiDocStmtAnalyzer $apiDocStmtAnalyzer,
private readonly Configuration $configuration
Expand Down Expand Up @@ -47,12 +53,12 @@ public function processNode(Node $node, Scope $scope): ?array
return null;
}

if ($this->apiDocStmtAnalyzer->isApiDoc($node, $classReflection)) {
$classLike = $node->getOriginalNode();
if (! $classLike instanceof Class_) {
return null;
}

$classLike = $node->getOriginalNode();
if (! $classLike instanceof Class_) {
if ($this->shouldSkipClass($classReflection, $classLike)) {
return null;
}

Expand All @@ -73,4 +79,15 @@ public function processNode(Node $node, Scope $scope): ?array

return $publicPropertyNames;
}

private function shouldSkipClass(ClassReflection $classReflection, Class_ $class): bool
{
foreach (self::CLASSES_TO_SKIP as $classToSkip) {
if ($classReflection->isSubclassOf($classToSkip)) {
return true;
}
}

return $this->apiDocStmtAnalyzer->isApiDoc($class, $classReflection);
}
}
8 changes: 8 additions & 0 deletions stubs/Livewire/Component.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Livewire;

class Component
{
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicPropertyRule\Fixture;

final class SkipLivewireComponent extends \Livewire\Component
{
public $available;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static function provideData(): Iterator

// laravel
yield [[__DIR__ . '/Fixture/SkipLaravelCommandSignatureProperty.php'], []];
yield [[__DIR__ . '/Fixture/SkipLivewireComponent.php'], []];

yield [
[
Expand Down

0 comments on commit 6fe7f27

Please sign in to comment.