Skip to content

Commit

Permalink
Merge branch 'master' into 2.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Sep 10, 2024
2 parents 3421778 + 1eb4502 commit 5b954f3
Show file tree
Hide file tree
Showing 58 changed files with 1,026 additions and 791 deletions.
1 change: 1 addition & 0 deletions monorepo/HydeStan/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
todo.md
60 changes: 60 additions & 0 deletions monorepo/HydeStan/HydeStan.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ final class HydeStan
private const TEST_FILE_ANALYSERS = [
NoFixMeAnalyser::class,
NoUsingAssertEqualsForScalarTypesTestAnalyser::class,
NoParentSetUpTearDownInUnitTestCaseAnalyser::class,
UnitTestCaseExtensionAnalyzer::class,
];

private const LINE_ANALYSERS = [
Expand Down Expand Up @@ -85,6 +87,8 @@ public function run(): void
$this->analyseFile($file, $this->getFileContents($file));
}

$this->console->info('Finished analyzing files!');

$this->runTestStan();

$endTime = microtime(true) - $time;
Expand Down Expand Up @@ -382,3 +386,59 @@ public function run(string $file, int $lineNumber, string $line): void
}
}
}

class NoParentSetUpTearDownInUnitTestCaseAnalyser extends FileAnalyser
{
public function run(string $file, string $contents): void
{
if (! str_contains($contents, 'extends UnitTestCase')) {
return;
}

$methods = ['setUp', 'tearDown'];

foreach ($methods as $method) {
AnalysisStatisticsContainer::analysedExpression();
if (str_contains($contents, "parent::$method()")) {
$lineNumber = substr_count(substr($contents, 0, strpos($contents, $method)), "\n") + 1;
$this->fail(sprintf("Found '%s' method in UnitTestCase at %s", "parent::$method()", fileLink($file, $lineNumber, false)));
HydeStan::addActionsMessage('error', $file, $lineNumber, "HydeStan: UnnecessaryParent{$method}MethodError", "{$method} method in UnitTestCase performs no operation and should be removed.");
}
}
}
}

class UnitTestCaseExtensionAnalyzer extends FileAnalyser
{
public function run(string $file, string $contents): void
{
// Check if the file is in the unit namespace
if (! str_contains($file, 'Unit')) {
AnalysisStatisticsContainer::analysedExpression();

return;
}

AnalysisStatisticsContainer::analysedExpression();

// Unit view tests are allowed to extend TestCase
if (str_contains($file, 'ViewTest')) {
AnalysisStatisticsContainer::analysedExpression();

return;
}

AnalysisStatisticsContainer::analysedExpression();

// Check if the class extends TestCase but not UnitTestCase
if (str_contains($contents, 'extends TestCase') && ! str_contains($contents, 'extends UnitTestCase')) {
AnalysisStatisticsContainer::analysedExpressions(2);

$lineNumber = substr_count(substr($contents, 0, strpos($contents, 'extends TestCase')), "\n") + 1;

todo(realpath(__DIR__.'/../../packages/framework/'.$file), $lineNumber, 'Refactor unit test to extend UnitTestCase instead of TestCase');

echo sprintf('Test in unit namespace extends TestCase instead of UnitTestCase at %s', fileLink($file, $lineNumber, false))."\n";
}
}
}
67 changes: 64 additions & 3 deletions monorepo/HydeStan/includes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ function check_str_contains_any(array $searches, string $line): bool
return $strContainsAny;
}

function fileLink(string $file, ?int $line = null): string
function fileLink(string $file, ?int $line = null, bool $substr = true): string
{
$path = (realpath(__DIR__.'/../../packages/framework/'.$file) ?: $file).($line ? ':'.$line : '');
$trim = strlen(getcwd()) + 2;
$path = substr($path, $trim);

if ($substr) {
$trim = strlen(getcwd()) + 2;
$path = substr($path, $trim);
}

return str_replace('\\', '/', $path);
}
Expand All @@ -74,3 +77,61 @@ function recursiveFileFinder(string $directory): array

return $files;
}

class TodoBuffer
{
private static array $todos = [];

public static function add(string $file, int $line, string $todo): void
{
self::$todos[] = [
'file' => $file,
'line' => $line,
'todo' => $todo,
];
}

public static function getTodos(): array
{
return self::$todos;
}

public static function writeTaskFile(): void
{
$todos = self::getTodos();

if (empty($todos)) {
return;
}

$taskFile = __DIR__.'/../todo.md';

$content = '# Todos'."\n\n";
$groupedTodos = [];

$baseDir = realpath(__DIR__.'/../../../');

foreach ($todos as $todo) {
$path = "{$todo['file']}:{$todo['line']}";
$path = str_replace('\\', '/', $path);

$path = substr($path, strlen($baseDir) + 1);

$groupedTodos[$todo['todo']][] = "[$path]($path)";
}

foreach ($groupedTodos as $todo => $items) {
$content .= "## $todo\n\n";
foreach ($items as $item) {
$content .= "- $item\n";
}
}

file_put_contents($taskFile, $content);
}
}

function todo(string $file, int $line, string $todo): void
{
TodoBuffer::add($file, $line, $todo);
}
3 changes: 3 additions & 0 deletions monorepo/HydeStan/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
$analyser = new HydeStan($debug);
$analyser->run();

// Todo: Could add a flag for this
TodoBuffer::writeTaskFile();

if ($analyser->hasErrors()) {
exit(1);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Facades/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static function unlinkIfExists(string $path): bool
*/
public static function getContents(string $path, bool $lock = false): string
{
return self::get($path, $lock);
return self::get(...func_get_args());
}

/**
Expand All @@ -126,7 +126,7 @@ public static function getContents(string $path, bool $lock = false): string
*/
public static function putContents(string $path, string $contents, bool $lock = false): bool|int
{
return self::put($path, $contents, $lock);
return self::put(...func_get_args());
}

protected static function filesystem(): \Illuminate\Filesystem\Filesystem
Expand Down
4 changes: 2 additions & 2 deletions packages/framework/src/Foundation/Kernel/FileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Hyde\Foundation\Kernel;

use Hyde\Facades\Filesystem;
use Hyde\Foundation\Concerns\BaseFoundationCollection;
use Hyde\Framework\Exceptions\FileNotFoundException;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Support\Filesystem\SourceFile;

use function basename;
use function glob;
use function str_starts_with;

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ protected function runExtensionHandlers(): void
protected function discoverFilesFor(string $pageClass): void
{
// Scan the source directory, and directories therein, for files that match the model's file extension.
foreach (glob($this->kernel->path($pageClass::sourcePath('{*,**/*}')), GLOB_BRACE) as $path) {
foreach (Filesystem::smartGlob($pageClass::sourcePath('{*,**/*}'), GLOB_BRACE) as $path) {
if (! str_starts_with(basename((string) $path), '_')) {
$this->addFile(SourceFile::make($path, $pageClass));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Hyde\Framework\Actions;

use Hyde\Hyde;
use RuntimeException;
use Hyde\Facades\Filesystem;

use function file_get_contents;
use function str_ends_with;
use function str_starts_with;
use function substr_count;
Expand Down Expand Up @@ -53,7 +52,7 @@ class BladeMatterParser

public static function parseFile(string $path): array
{
return static::parseString(file_get_contents(Hyde::path($path)));
return static::parseString(Filesystem::getContents($path));
}

public static function parseString(string $contents): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;
namespace Hyde\Framework\Testing\Feature;

use Hyde\Facades\Filesystem;
use Hyde\Foundation\Facades\Routes;
Expand Down
Loading

0 comments on commit 5b954f3

Please sign in to comment.