Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</include>
<exclude>
<file>src/bootstrap.php</file>
<directory suffix=".php">src/internal/Cli</directory>
<directory>src/Internal/Cli</directory>
</exclude>
</source>
</phpunit>
5 changes: 5 additions & 0 deletions src/Internal/Cli/CommandRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class CommandRunner
private const KNOWN_COMMANDS = [
'config:init',
'report',
'report:merge',
'report:clear',
'cache:clear',
'cache:warm',
Expand Down Expand Up @@ -47,6 +48,10 @@ public static function run(array $args, $outputStream = STDOUT, $errorStream = S
return (new ReportCommand())->execute($args, $outputStream, $errorStream);
}

if ($firstArg === 'report:merge') {
return (new ReportMergeCommand())->execute($args, $outputStream, $errorStream);
}

if ($firstArg === 'report:clear') {
return (new ReportClearCommand())->execute($args, $outputStream, $errorStream);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Cli/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function execute(array $args, $outputStream = STDOUT, $errorStream = STDE
fwrite($outputStream, ' ' . $c('COMMANDS', 'yellow') . "\n");
fwrite($outputStream, ' ' . $c('config:init', 'green') . " Generate default typephp.php configuration file\n");
fwrite($outputStream, ' ' . $c('report', 'green') . " Display audit report summary in terminal\n");
fwrite($outputStream, ' ' . $c('report:merge', 'green') . " Merge multiple JSON report files into a unified report\n");
fwrite($outputStream, ' ' . $c('report:clear', 'green') . " Delete generated report file and shards\n");
fwrite($outputStream, ' ' . $c('cache:clear', 'green') . " Clear all cached transformed files\n");
fwrite($outputStream, ' ' . $c('cache:warm', 'green') . " Pre-transform and warm up cache for included files\n");
Expand All @@ -24,6 +25,7 @@ public function execute(array $args, $outputStream = STDOUT, $errorStream = STDE
fwrite($outputStream, ' ' . $c('EXAMPLES', 'yellow') . "\n");
fwrite($outputStream, " vendor/bin/typephp config:init\n");
fwrite($outputStream, " vendor/bin/typephp report\n");
fwrite($outputStream, " vendor/bin/typephp report:merge module1.json module2.json --output=all.json\n");
fwrite($outputStream, " vendor/bin/typephp index.php\n");
fwrite($outputStream, " vendor/bin/typephp cache:rebuild\n\n");

Expand Down
14 changes: 13 additions & 1 deletion src/Internal/Cli/ReportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,24 @@ public function execute(array $args, $outputStream = STDOUT, $errorStream = STDE
$target = isset($item['target']) && \is_string($item['target']) ? $item['target'] : '';
$expected = isset($item['expected']) && \is_string($item['expected']) ? $item['expected'] : '';
$given = isset($item['given']) && \is_string($item['given']) ? $item['given'] : '';
$count = isset($item['count']) && \is_int($item['count']) ? $item['count'] : 1;
$caller = isset($item['caller']) && \is_string($item['caller']) ? $item['caller'] : null;
$declaredIn = isset($item['declared_in']) && \is_string($item['declared_in']) ? $item['declared_in'] : null;

$lineFormatted = str_pad("Line {$line}", 10);
$kindFormatted = str_pad($kind, 11);
$targetFormatted = str_pad($target, 12);
$countFormatted = $count > 1 ? $c(" (x{$count})", 'yellow') : '';

fwrite($outputStream, ' ' . $c($lineFormatted, 'gray') . ' ' . $c($kindFormatted, 'yellow') . ' ' . $c($targetFormatted, 'cyan') . ' expected ' . $c($expected, 'green') . ', ' . $c($given, 'red') . "\n");
fwrite($outputStream, ' ' . $c($lineFormatted, 'gray') . ' ' . $c($kindFormatted, 'yellow') . ' ' . $c($targetFormatted, 'cyan') . ' expected ' . $c($expected, 'green') . ', ' . $c($given, 'red') . $countFormatted . "\n");

if ($caller !== null) {
fwrite($outputStream, ' ' . $c('↳ caller:', 'gray') . ' ' . $c($caller, 'gray') . "\n");
}

if ($declaredIn !== null) {
fwrite($outputStream, ' ' . $c('↳ declared in:', 'gray') . ' ' . $c($declaredIn, 'gray') . "\n");
}
}

fwrite($outputStream, "\n");
Expand Down
132 changes: 132 additions & 0 deletions src/Internal/Cli/ReportMergeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

declare(strict_types=1);

namespace TypePHP\Internal\Cli;

use TypePHP\Internal\Reporting\ViolationRecord;

/**
* @internal Merges multiple JSON report files into a unified report with aggregated occurrence counts.
*/
final class ReportMergeCommand implements CommandInterface
{
public function execute(array $args, $outputStream = STDOUT, $errorStream = STDERR): int
{
$c = [CliFormatter::class, 'color'];

$inputFiles = [];
$outputFile = null;

for ($i = 0; $i < \count($args); $i++) {
$arg = $args[$i];

if ($arg === 'report:merge') {
continue;
}

if (str_starts_with($arg, '--output=')) {
$outputFile = substr($arg, 9);

continue;
}

if ($arg === '-o' && isset($args[$i + 1])) {
$outputFile = $args[$i + 1];
$i++;

continue;
}

if (! str_starts_with($arg, '-')) {
$inputFiles[] = $arg;
}
}

if ($inputFiles === []) {
fwrite($errorStream, "\n " . $c(' TYPEPHP ', 'badge_red') . ' ' . $c('Error', 'bold') . "\n\n");
fwrite($errorStream, ' ' . $c('✗', 'red') . " No input report files specified.\n\n");
fwrite($errorStream, ' ' . $c('Usage:', 'yellow') . "\n");
fwrite($errorStream, " vendor/bin/typephp report:merge <file1.json> <file2.json> [--output=<merged.json>]\n\n");

return 1;
}

/** @var array<string, ViolationRecord> $mergedViolations */
$mergedViolations = [];
$validFileCount = 0;

foreach ($inputFiles as $file) {
if (! file_exists($file)) {
fwrite($errorStream, ' ' . $c('!', 'yellow') . ' Warning: Report file ' . $c('"' . $file . '"', 'bold') . " not found, skipping.\n");

continue;
}

$rawContent = @file_get_contents($file);
if ($rawContent === false || trim($rawContent) === '') {
continue;
}

/** @var array{violations?: list<array<string, mixed>>}|null $data */
$data = json_decode($rawContent, true);
if (! \is_array($data) || ! isset($data['violations']) || ! \is_array($data['violations'])) {
continue;
}

$validFileCount++;

foreach ($data['violations'] as $v) {
if (\is_array($v)) {
$record = ViolationRecord::fromArray($v);
if ($record !== null) {
$hash = $record->getHash();
if (isset($mergedViolations[$hash])) {
$mergedViolations[$hash] = $mergedViolations[$hash]->withIncrementedCount($record->count);
} else {
$mergedViolations[$hash] = $record;
}
}
}
}
}

$violationsList = array_values($mergedViolations);
$filesAffected = [];
foreach ($violationsList as $v) {
$filesAffected[$v->file] = true;
}

$document = [
'version' => '1.0',
'generated_at' => gmdate('c'),
'summary' => [
'total_violations' => \count($violationsList),
'files_affected' => \count($filesAffected),
],
'violations' => $violationsList,
];

$json = json_encode($document, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

if ($outputFile !== null && $json !== false) {
$outputDir = \dirname($outputFile);
if (! is_dir($outputDir)) {
@mkdir($outputDir, 0777, true);
}

file_put_contents($outputFile, $json);

fwrite($outputStream, "\n " . $c(' TYPEPHP ', 'badge') . ' ' . $c('Report Merge', 'bold') . "\n\n");
fwrite($outputStream, ' ' . $c('✓', 'green') . ' Merged ' . $c((string) $validFileCount, 'bold') . ' report file(s) into ' . $c('"' . $outputFile . '"', 'bold') . ' (' . $c((string) \count($violationsList), 'bold') . " unique violation(s)).\n\n");

return 0;
}

if ($json !== false) {
fwrite($outputStream, $json . "\n");
}

return 0;
}
}
9 changes: 2 additions & 7 deletions src/Internal/Docblock/DocblockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public static function parseMagicPropertyContract(string $className, string $pro
*
* @return array{doc: string, declaringClass: \ReflectionClass<object>}|null
*/
private static function findDeclaredPropertyDoc(\ReflectionClass $refClass, string $propertyName): ?array
public static function findDeclaredPropertyDoc(\ReflectionClass $refClass, string $propertyName): ?array
{
$current = $refClass;
while ($current !== false) {
Expand Down Expand Up @@ -1227,14 +1227,9 @@ private static function parseClassLevelDocs(\ReflectionClass $declaringClass, ar

foreach ($classHierarchy as $hierClass) {
$hierClassName = $hierClass->getName();
$fileName = $hierClass->getFileName();
$stubDoc = StubManager::getClassDoc($hierClassName);

if ($stubDoc === null && FileFilter::isFileExcluded($fileName !== false ? $fileName : null)) {
continue;
}

$classDoc = $stubDoc ?? $hierClass->getDocComment();

if ($classDoc !== false && $classDoc !== null) {
$classPhpDocNode = DocblockExtractor::parseDocString($classDoc);

Expand Down
9 changes: 0 additions & 9 deletions src/Internal/Generics/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
use TypePHP\Internal\Resolver\SpecialTypeResolver;
use TypePHP\Internal\Util\ClassNameValidator;
use TypePHP\Internal\Util\Config;
use TypePHP\Internal\Util\FileFilter;
use TypePHP\Internal\Util\StubManager;
use WeakMap;

Expand Down Expand Up @@ -1033,14 +1032,6 @@ private static function computeClassInheritedBindings(string $actualClassName):
$classHierarchy = HierarchyResolver::getClassHierarchy($ref);

foreach ($classHierarchy as $hierClass) {
$fileName = $hierClass->getFileName();
$hierClassName = $hierClass->getName();
$stubDoc = StubManager::getClassDoc($hierClassName);

if ($stubDoc === null && $hierClass->getName() !== $actualClassName && FileFilter::isFileExcluded($fileName !== false ? $fileName : null)) {
continue;
}

$docsToInspect = self::collectDocsForClassHierarchyMember($hierClass);

foreach ($docsToInspect as $rawDoc) {
Expand Down
Loading
Loading