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

new Insight: Dirty Class Detector #2

Closed
szepeviktor opened this issue Apr 1, 2019 · 8 comments
Closed

new Insight: Dirty Class Detector #2

szepeviktor opened this issue Apr 1, 2019 · 8 comments

Comments

@szepeviktor
Copy link
Contributor

szepeviktor commented Apr 1, 2019

/**
 * Dirty Class Detector.
 *
 * @package Dirtyclassdetector
 */

use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;

require __DIR__.'/vendor/autoload.php';

$code = file_get_contents($argv[1]);

$hasSideEffect = false;

$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
    $ast = $parser->parse($code);
} catch (Error $error) {
    echo "Parse error: {$error->getMessage()}\n";
    exit(1);
}

$cleaner = new NodeTraverser;
$cleaner->addVisitor(new class extends NodeVisitorAbstract {
    public function leaveNode(Node $node) {
        // TODO class+trait+interface count must be 1, declare count 0-1, group/use 0+
        if (
            $node instanceof Node\Stmt\Interface_
            || $node instanceof Node\Stmt\Trait_
            || $node instanceof Node\Stmt\Class_
            || $node instanceof Node\Stmt\Use_
            || $node instanceof Node\Stmt\GroupUse
            || $node instanceof Node\Stmt\Declare_
        ) {
            return NodeTraverser::REMOVE_NODE;
        }
    }
});
$cleaned = $cleaner->traverse($ast);

$detector = new NodeTraverser;
$detector->addVisitor(new class extends NodeVisitorAbstract {
    public function leaveNode(Node $node) {
        global $hasSideEffect;
        // TODO namespace 0-1, name 1 per namespace
        if ($node instanceof Node\Stmt\Namespace_ || $node instanceof Node\Name) {
            return;
        }
        echo "Side effect: {$node->getType()}\n";
        $hasSideEffect = true;
    }
});
$detector->traverse($cleaned);

if ($hasSideEffect) {
    exit(2);
}
@szepeviktor
Copy link
Contributor Author

Uses nikic/php-parser

@nunomaduro
Copy link
Owner

Could you summarise what this code it's doing? It's not clear for me. 🤔

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Apr 1, 2019

namespace boo;

class dirt {
	function foo() {
		sleep(1);
	}
}
echo "I'm a junior developer";
  • it parses the code
  • removes interface/class/etc. and everything in it
  • if the remaining thing is not a namespace statement it alerts you: "Dirty Class"

@szepeviktor
Copy link
Contributor Author

szepeviktor commented Apr 1, 2019

I meet new code bases every month an I need this tool to make sure each file contains only 1 class.

@nunomaduro
Copy link
Owner

I see. It's a good Insight. I will add it!

@szepeviktor
Copy link
Contributor Author

Thank you.

@nunomaduro
Copy link
Owner

There is already an Insight for this.

@szepeviktor
Copy link
Contributor Author

Very well!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants