|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Latte (https://latte.nette.org) |
| 5 | + * Copyright (c) 2008 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Latte\Tools; |
| 11 | + |
| 12 | +use Latte; |
| 13 | +use Latte\Compiler\Node; |
| 14 | +use Latte\Compiler\Nodes\Php; |
| 15 | +use Latte\Compiler\Nodes\Php\Expression; |
| 16 | +use Latte\Compiler\Nodes\TemplateNode; |
| 17 | +use Latte\Compiler\NodeTraverser; |
| 18 | +use function defined; |
| 19 | + |
| 20 | + |
| 21 | +/** |
| 22 | + * Linter extension for validating filters, functions, classes, methods, constants and more. |
| 23 | + */ |
| 24 | +final class LinterExtension extends Latte\Extension |
| 25 | +{ |
| 26 | + private ?Latte\Engine $engine = null; |
| 27 | + |
| 28 | + |
| 29 | + public function beforeCompile(Latte\Engine $engine): void |
| 30 | + { |
| 31 | + $this->engine = $engine; |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + public function getPasses(): array |
| 36 | + { |
| 37 | + return [ |
| 38 | + 'linter' => $this->linterPass(...), |
| 39 | + ]; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + private function linterPass(TemplateNode $node): void |
| 44 | + { |
| 45 | + (new NodeTraverser)->traverse($node, function (Node $node) { |
| 46 | + if ($node instanceof Php\FilterNode) { |
| 47 | + $this->validateFilter($node); |
| 48 | + |
| 49 | + } elseif ($node instanceof Expression\FunctionCallNode && $node->name instanceof Php\NameNode) { |
| 50 | + $this->validateFunction($node); |
| 51 | + |
| 52 | + } elseif ($node instanceof Expression\NewNode && $node->class instanceof Php\NameNode) { |
| 53 | + $this->validateClass($node); |
| 54 | + |
| 55 | + } elseif ($node instanceof Expression\StaticMethodCallNode |
| 56 | + && $node->class instanceof Php\NameNode |
| 57 | + && $node->name instanceof Php\IdentifierNode |
| 58 | + ) { |
| 59 | + $this->validateStaticMethod($node); |
| 60 | + |
| 61 | + } elseif ($node instanceof Expression\ClassConstantFetchNode |
| 62 | + && $node->class instanceof Php\NameNode |
| 63 | + && $node->name instanceof Php\IdentifierNode |
| 64 | + ) { |
| 65 | + $this->validateClassConstant($node); |
| 66 | + |
| 67 | + } elseif ($node instanceof Expression\ConstantFetchNode) { |
| 68 | + $this->validateConstant($node); |
| 69 | + |
| 70 | + } elseif ($node instanceof Expression\InstanceofNode && $node->class instanceof Php\NameNode) { |
| 71 | + $this->validateInstanceof($node); |
| 72 | + |
| 73 | + } elseif ($node instanceof Expression\StaticPropertyFetchNode |
| 74 | + && $node->class instanceof Php\NameNode |
| 75 | + && $node->name instanceof Php\VarLikeIdentifierNode |
| 76 | + ) { |
| 77 | + $this->validateStaticProperty($node); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private function validateFilter(Php\FilterNode $node): void |
| 84 | + { |
| 85 | + $name = $node->name->name; |
| 86 | + $filters = $this->engine->getFilters(); |
| 87 | + if (!isset($filters[$name])) { |
| 88 | + trigger_error("Unknown filter |$name $node->position", E_USER_WARNING); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + private function validateFunction(Expression\FunctionCallNode $node): void |
| 94 | + { |
| 95 | + $name = (string) $node->name; |
| 96 | + if (!function_exists($name)) { |
| 97 | + trigger_error("Unknown function $name() $node->position", E_USER_WARNING); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + private function validateClass(Expression\NewNode $node): void |
| 103 | + { |
| 104 | + $className = (string) $node->class; |
| 105 | + if (!class_exists($className) && !interface_exists($className)) { |
| 106 | + trigger_error("Unknown class $className $node->position", E_USER_WARNING); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + private function validateStaticMethod(Expression\StaticMethodCallNode $node): void |
| 112 | + { |
| 113 | + $className = (string) $node->class; |
| 114 | + $methodName = $node->name->name; |
| 115 | + if (!method_exists($className, $methodName)) { |
| 116 | + trigger_error("Unknown method $className::$methodName() $node->position", E_USER_WARNING); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + private function validateClassConstant(Expression\ClassConstantFetchNode $node): void |
| 122 | + { |
| 123 | + $name = "{$node->class}::{$node->name->name}"; |
| 124 | + if (!defined($name)) { |
| 125 | + trigger_error("Unknown class constant $name $node->position", E_USER_WARNING); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + |
| 130 | + private function validateConstant(Expression\ConstantFetchNode $node): void |
| 131 | + { |
| 132 | + $magic = ['__LINE__' => 1, '__FILE__' => 1, '__DIR__' => 1]; |
| 133 | + $name = (string) $node->name; |
| 134 | + if (!defined($name) && !isset($magic[$name])) { |
| 135 | + trigger_error("Unknown constant $name $node->position", E_USER_WARNING); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + private function validateInstanceof(Expression\InstanceofNode $node): void |
| 141 | + { |
| 142 | + $className = (string) $node->class; |
| 143 | + if (!class_exists($className) && !interface_exists($className)) { |
| 144 | + trigger_error("Unknown class $className in instanceof $node->position", E_USER_WARNING); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + private function validateStaticProperty(Expression\StaticPropertyFetchNode $node): void |
| 150 | + { |
| 151 | + $className = (string) $node->class; |
| 152 | + $propertyName = $node->name->name; |
| 153 | + if (!property_exists($className, $propertyName)) { |
| 154 | + trigger_error("Unknown static property $className::\$$propertyName $node->position", E_USER_WARNING); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments