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
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
->setFinder(
(new Finder())
->in(__DIR__)
->notPath('tests/Components/WeirdFormattingDefaultArgs.php')
)
;
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
beStrictAboutOutputDuringTests="true"
failOnRisky="true"
failOnWarning="true"
stopOnError="true"
stopOnError="false"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="false"
Expand Down
190 changes: 190 additions & 0 deletions src/Analyzer/Extractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

declare(strict_types=1);

namespace Exan\Moock\Analyzer;

/**
* @internal
*
* I am not a language dev, please don't judge me too harshly for this poor excuse of a parser :)
*
* Valid PHP can be assumed, as the files have gone through reflection already prior to reaching this stage.
*/
class Extractor
{
/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, string|array{0:int, 1:string, 2:int}>
*/
public static function lines(array $tokens, int $start, int $end): array
{
$capture = false;
$captured = [];

foreach ($tokens as $token) {
if (!$capture && is_array($token)) {
$isWithinLines = $token[2] >= $start && $token[2] <= $end;

if (!$capture && $isWithinLines) {
$capture = true;
} elseif ($capture && !$isWithinLines) {
break;
}
}

if ($capture) {
$captured[] = $token;
}
}

return $captured;
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, string|array{0:int, 1:string, 2:int}>
*/
public static function function(array $tokens, string $functionName): array
{
$capture = false;
$captured = [];

foreach ($tokens as $i => $token) {
if (
is_array($token)
&& $token[0] === T_FUNCTION
&& is_array($tokens[$i + 2])
&& $tokens[$i + 2][0] === T_STRING
) {
$isRequestedFunction = $tokens[$i + 2][1] === $functionName;

if (!$capture && $isRequestedFunction) {
$capture = true;
} elseif ($capture && !$isRequestedFunction) {
break;
}
}

if ($capture) {
$captured[] = $token;
}
}

while (
count($captured)
&& is_array($captured[count($captured) - 1])
&& in_array(
$captured[count($captured) - 1][0],
[T_WHITESPACE, T_PUBLIC, T_PROTECTED, T_PRIVATE]
)
) {
array_pop($captured);
}

return $captured;
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, string|array{0:int, 1:string, 2:int}>
*/
public static function arg(array $tokens, string $argName): array
{
$argName = '$' . trim($argName, '$');
$capture = false;
$captured = [];

$trimToLast = ')';

foreach ($tokens as $token) {
if (is_array($token)
&& $token[0] === T_VARIABLE
) {
$isRequestedArg = $token[1] === $argName;

if (!$capture && $isRequestedArg) {
$capture = true;
} elseif ($capture && !$isRequestedArg) {
$trimToLast = ',';
break;
}
}

if ($token === '{') {
break;
}

if ($capture) {
$captured[] = $token;
}
}

while (
count($captured)
&& array_pop($captured) !== $trimToLast
) {
}

return $captured;
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, string|array{0:int, 1:string, 2:int}>
*/
public static function uses(array $tokens): array
{
$uses = [];
$use = null;

$blockScope = 0;

foreach ($tokens as $token) {
if ($token === '{') {
$blockScope++;
}

if ($token === '}') {
$blockScope--;
}

if ($token === ';') {
$use = null;
}

if ($blockScope === 0 && is_array($token) && $token[0] === T_USE) {
$use = count($uses);
$uses[] = [];
}

if ($use !== null) {
$uses[$use][] = $token;
}
}

return $uses;
}

public static function namespace(array $tokens): array
{
$namespace = [];
$capturing = false;

foreach ($tokens as $token) {
if (is_array($token) && $token[0] === T_NAMESPACE) {
$capturing = true;
}

if ($capturing && $token === ';') {
break;
}

if ($capturing) {
$namespace[] = $token;
}
}

return $namespace;
}
}
177 changes: 177 additions & 0 deletions src/Analyzer/Utilize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

declare(strict_types=1);

namespace Exan\Moock\Analyzer;

/**
* @internal
*
* I am not a language dev, please don't judge me too harshly for this poor excuse of a parser :)
*
* Valid PHP can be assumed, as the files have gone through reflection already prior to reaching this stage.
*/
class Utilize
{
/**
* @param ?string $declaringNamespace
* @param array<string, string> $uses
*/
public function __construct(
private readonly ?string $declaringNamespace,
private readonly array $uses,
) {}

public function fullyQuantify(string $className): string
{
if (isset($this->uses[$className])) {
return $this->format($this->uses[$className]);
}

return $this->format(($this->declaringNamespace ?? '') . '\\' . $className);
}

private function format(string $fullyQualifiedClass): string
{
return '\\' . trim($fullyQualifiedClass, '\\');
}

/**
* @param string $declaringNamespace
* @param array<int, array<int, string|array{0:int, 1:string, 2:int}>> $uses
*/
public static function fromTokens(?string $declaringNamespace, array $uses): static
{
$convertedUses = array_merge(...array_map(static::parseLine(...), $uses));

return new static($declaringNamespace, array_merge(...$convertedUses));
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $line
*/
private static function parseLine($line): array
{
array_shift($line); // use
array_shift($line); // (whitespace)

return array_map(static::parseIndividualUse(...), static::individualUses($line));
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, array<int, string|array{0:int, 1:string, 2:int}>>
*/
private static function individualUses($tokens): array
{
$individual = [[]];
$blockLevel = 0;
$i = 0;

foreach ($tokens as $token) {
if ($token === '{') {
$blockLevel++;
}

if ($token === '}') {
$blockLevel--;
}

if ($blockLevel === 0 && $token === ',') {
$individual[] = [];
$i++;
continue;
}

$individual[$i][] = $token;
}

return $individual;
}

private static function parseIndividualUse(array $use): array
{
while (is_array($use[0]) && $use[0][0] === T_WHITESPACE) {
array_shift($use);
}

if (count($use) === 1) {
// use Some\Potentially\Namespaced\Class
$split = explode('\\', $use[0][1]);
return [array_pop($split) => $use[0][1]];
}

if (count($use) === 5 && is_array($use[2]) && $use[2][1] === 'as') {
// use Some\Potentially\Namespaced\Class as Alias
$trueImport = array_shift($use);
$alias = array_pop($use);

return [$alias[1] => $trueImport[1]];
}

// use Some\Potentially\Namespaced\{Class, Or\Other\Class}
return static::parseGroupUse($use);
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
*/
private static function parseGroupUse(array $tokens): array
{
$namespacePrefix = '';
while (count($tokens) && $tokens[0] !== '{') {
$token = array_shift($tokens);

$namespacePrefix .= (is_array($token) ? $token[1] : $token);
}

array_shift($tokens); // {
array_pop($tokens); // }

$imports = static::splitGroupUse($tokens);

return array_merge(...array_map(function (array $tokens) use ($namespacePrefix) {
while (count($tokens) && is_array($tokens) && $tokens[0][0] === T_WHITESPACE) {
array_shift($tokens);
}

$fullImport = $namespacePrefix . $tokens[0][1];

if (count($tokens) < 3) { // Not aliased, possibly followed by whitespace
$split = explode('\\', $fullImport);

$alias = array_pop($split);
} else { // Aliased
$alias = $tokens[4][1];
}

return [$alias => $fullImport];
}, $imports));
}

/**
* @param array<int, string|array{0:int, 1:string, 2:int}> $tokens
* @return array<int, array<int, string|array{0:int, 1:string, 2:int}>>
*/
private static function splitGroupUse(array $tokens): array
{
$imports = [[]];
$i = 0;

foreach ($tokens as $token) {
if ($token === '}') {
break;
}

if ($token === ',') {
$imports[] = [];
$i++;
continue;
}

$imports[$i][] = $token;
}

return $imports;
}
}
1 change: 0 additions & 1 deletion src/Formatting/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionUnionType;

Expand Down
Loading
Loading