|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Liquid package. |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + * |
| 9 | + * @package Liquid |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Liquid; |
| 13 | + |
| 14 | +use ReflectionClass; |
| 15 | +use function array_filter; |
| 16 | +use function class_exists; |
| 17 | +use function file; |
| 18 | +use function interface_exists; |
| 19 | +use function join; |
| 20 | +use function trait_exists; |
| 21 | +use function var_dump; |
| 22 | + |
| 23 | +/** |
| 24 | + * @coversNothing |
| 25 | + */ |
| 26 | +class StaticAnalysisTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @return iterable |
| 30 | + */ |
| 31 | + public static function provideClasses() |
| 32 | + { |
| 33 | + $files = require 'vendor/composer/autoload_classmap.php'; |
| 34 | + |
| 35 | + $seenNonVendor = false; |
| 36 | + foreach ($files as $class => $filename) { |
| 37 | + $path = str_replace(getcwd(), '.', $filename); |
| 38 | + |
| 39 | + if (strpos($path, './vendor/') === 0) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + $seenNonVendor = true; |
| 44 | + yield $class => [str_replace(getcwd(), '.', $filename), $class]; |
| 45 | + } |
| 46 | + |
| 47 | + if ($seenNonVendor === false) { |
| 48 | + throw new \RuntimeException('Please generate the classmap.'); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @dataProvider provideClasses |
| 54 | + * @param mixed $filename |
| 55 | + * @param mixed $class |
| 56 | + */ |
| 57 | + public function testClassExists($filename, $class) |
| 58 | + { |
| 59 | + $this->assertTrue(class_exists($class) || trait_exists($class) || interface_exists($class)); |
| 60 | + } |
| 61 | + |
| 62 | + public static function provideAbstractTagSubclasses() |
| 63 | + { |
| 64 | + foreach (self::provideClasses() as $class => $data) { |
| 65 | + if (!is_subclass_of($class, AbstractTag::class)) { |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + $refClass = new ReflectionClass($class); |
| 70 | + if (!$refClass->hasMethod('__construct')) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if ($refClass->getMethod('__construct')->getDeclaringClass()->getName() === AbstractTag::class) { |
| 75 | + continue; // Skip if the constructor is not overridden |
| 76 | + } |
| 77 | + |
| 78 | + yield $class => [...$data, $refClass]; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dataProvider provideAbstractTagSubclasses |
| 84 | + * @param string $filename |
| 85 | + * @param class-string<AbstractTag> $class |
| 86 | + * @param ReflectionClass<AbstractTag> $refClass |
| 87 | + */ |
| 88 | + public function testAbstractTagChildCallsConstruct($filename, $class, ReflectionClass $refClass) |
| 89 | + { |
| 90 | + $refMethod = $refClass->getMethod('__construct'); |
| 91 | + $startLine = $refMethod->getStartLine(); |
| 92 | + $endLine = $refMethod->getEndLine(); |
| 93 | + |
| 94 | + $code = array_slice(file($refMethod->getFileName()), $startLine - 1, $endLine - $startLine + 1); |
| 95 | + |
| 96 | + $this->assertNotEmpty($code); |
| 97 | + |
| 98 | + $linesWithConstruct = array_filter($code, function ($line) { |
| 99 | + return strpos($line, 'parent::__construct') !== false; |
| 100 | + }); |
| 101 | + |
| 102 | + $this->assertNotEmpty( |
| 103 | + $linesWithConstruct, |
| 104 | + "The constructor of {$refClass->getName()} should call parent::__construct()" |
| 105 | + ); |
| 106 | + } |
| 107 | +} |
0 commit comments