Skip to content

Commit

Permalink
Merge pull request #650 from phel-lang/refactor/string-printer
Browse files Browse the repository at this point in the history
Refactor StringPrinter
  • Loading branch information
Chemaclass committed Jan 13, 2024
2 parents 38614e5 + bd1241a commit 4fc115b
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -41,7 +41,7 @@ jobs:
run: ./vendor/bin/php-cs-fixer fix --allow-risky=yes --dry-run --show-progress=dots --using-cache=no --verbose

- name: Run rector/rector
run: ./vendor/bin/php-cs-fixer fix --dry-run --show-progress=dots --using-cache=no --verbose
run: ./vendor/bin/rector process --dry-run --config=rector.php

type-checker:
name: Type Checker
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -38,7 +38,7 @@
"psalm/plugin-phpunit": "^0.18",
"symfony/var-dumper": "^7.0",
"vimeo/psalm": "^5.19",
"rector/rector": "^0.18"
"rector/rector": "^0.17"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 9 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions rector.php
Expand Up @@ -3,10 +3,10 @@
declare(strict_types=1);

use Rector\CodeQuality\Rector\If_\SimplifyIfElseToTernaryRector;
use Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector;
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\Core\Collector\MockedClassCollector;
use Rector\Core\Collector\ParentClassCollector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\PHPUnit\CodeQuality\Rector\Class_\PreferPHPUnitThisCallRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Privatization\Rector\Class_\FinalizeClassesWithoutChildrenRector;
Expand Down Expand Up @@ -52,8 +52,13 @@
__DIR__ . '/tests/php/Unit/Printer/TypePrinter/StubStruct.php',
],

ReadOnlyPropertyRector::class => [
__DIR__ . '/src/php/Lang/Collections/Map/TransientHashMap.php',
],

DisallowedShortTernaryRuleFixerRector::class,
PreferPHPUnitThisCallRector::class,
UnSpreadOperatorRector::class,
]);

$rectorConfig->sets([
Expand All @@ -70,9 +75,6 @@
PHPUnitSetList::PHPUNIT_90,
]);

$rectorConfig->collector(ParentClassCollector::class);
$rectorConfig->collector(MockedClassCollector::class);

$rectorConfig->importNames();
$rectorConfig->removeUnusedImports();
};
2 changes: 1 addition & 1 deletion src/php/Build/Domain/Compile/DependenciesForNamespace.php
Expand Up @@ -36,7 +36,7 @@ public function getDependenciesForNamespace(array $directories, array $ns): arra
while ($queue !== []) {
$currentNs = array_shift($queue);

if (array_key_exists($currentNs, $requiredNamespaces) === false
if (!array_key_exists($currentNs, $requiredNamespaces)
&& array_key_exists($currentNs, $index)
) {
foreach ($index[$currentNs]->getDependencies() as $depNs) {
Expand Down
2 changes: 1 addition & 1 deletion src/php/Command/Domain/Handler/ExceptionHandler.php
Expand Up @@ -71,7 +71,7 @@ private function registerCustomErrorHandler(): void
$errstr,
$errfile,
$errline,
json_encode([...$errcontext, 'errno' => $errno]),
json_encode([...$errcontext, 'errno' => $errno], JSON_THROW_ON_ERROR),
);

if (self::$previousTextError !== $text) {
Expand Down
Expand Up @@ -104,7 +104,7 @@ public function getStackTraceString(Throwable $e): string
$file = $frame['file'] ?? 'unknown_file';
$line = $frame['line'] ?? 0;

if ($class) {
if ($class !== null) {
$rf = new ReflectionClass($class);
if ($rf->implementsInterface(FnInterface::class)) {
$boundTo = $rf->getConstant('BOUND_TO');
Expand Down
Expand Up @@ -48,7 +48,7 @@ private function methods(?PersistentListInterface $list): array
}

$methods = [];
for ($forms = $list; $forms !== null; $forms = $forms->cdr()) {
for ($forms = $list; $forms instanceof PersistentListInterface; $forms = $forms->cdr()) {
$first = $forms->first();

if (!$first instanceof PersistentListInterface) {
Expand Down
Expand Up @@ -120,7 +120,7 @@ private function interfaces(PersistentListInterface $list, NodeEnvironmentInterf
$countExpectedMethods = count($expectedMethods);
for ($i = 0; $i < $countExpectedMethods; ++$i) {
$forms = $forms->cdr();
if ($forms === null) {
if (!$forms instanceof PersistentListInterface) {
throw AnalyzerException::withLocation('Missing method for interface ' . $absoluteInterfaceName . ' in defstruct', $list);
}

Expand Down
Expand Up @@ -26,7 +26,7 @@ public function analyze(PersistentListInterface $list, NodeEnvironmentInterface
$catches = [];
/** @var PersistentListInterface|null $finally */
$finally = null;
for ($forms = $list->cdr(); $forms !== null; $forms = $forms->cdr()) {
for ($forms = $list->cdr(); $forms instanceof PersistentListInterface; $forms = $forms->cdr()) {
/** @var mixed $form */
$form = $forms->first();

Expand Down
Expand Up @@ -5,6 +5,7 @@
namespace Phel\Compiler\Domain\Emitter\OutputEmitter\NodeEmitter;

use Phel\Compiler\Domain\Analyzer\Ast\FnNode;
use Phel\Lang\Collections\Map\PersistentMapInterface;
use Phel\Lang\Keyword;
use Phel\Lang\Symbol;

Expand Down Expand Up @@ -36,7 +37,7 @@ private function emitMethodParameters(FnNode $node): void
$this->outputEmitter->emitPhpVariable($symbol, $loc = null, $asReference = false, $isVariadic = true);
} else {
$meta = $symbol->getMeta();
$isReference = $meta && $meta->find(Keyword::create('reference')) === true;
$isReference = $meta instanceof PersistentMapInterface && $meta->find(Keyword::create('reference')) === true;
$this->outputEmitter->emitPhpVariable($symbol, $loc = null, $isReference);
}

Expand Down
2 changes: 1 addition & 1 deletion src/php/Compiler/Domain/Reader/QuasiquoteTransformer.php
Expand Up @@ -159,7 +159,7 @@ private function isLiteral($x): bool
|| is_float($x)
|| is_int($x)
|| is_bool($x)
|| $x === null
|| !$x instanceof TypeInterface
|| $x instanceof Keyword;
}

Expand Down
8 changes: 4 additions & 4 deletions src/php/Formatter/Domain/Rules/Zipper/AbstractZipper.php
Expand Up @@ -171,8 +171,8 @@ public function up(): self
*/
public function root()
{
if ($this->isEnd()) {
return $this->getNode();
if ($this->isEnd) {
return $this->node;
}

$loc = $this;
Expand Down Expand Up @@ -214,7 +214,7 @@ public function down(): self
*/
public function next(): self
{
if ($this->isEnd()) {
if ($this->isEnd) {
return $this;
}

Expand Down Expand Up @@ -379,7 +379,7 @@ public function remove(): self
true,
false,
);
while ($loc->isBranch() && $loc->hasChildren() && ($child = $loc->down())) {
while ($loc->isBranch() && $loc->hasChildren() && (($child = $loc->down()) instanceof self)) {
$loc = $child->rightMost();
}

Expand Down
2 changes: 1 addition & 1 deletion src/php/Lang/Collections/LinkedList/PersistentList.php
Expand Up @@ -126,7 +126,7 @@ public function equals(mixed $other): bool
for ($s = $this; $s != null; $s = $s->cdr(), $ms = $ms->cdr()) {
/** @var PersistentList $s */
/** @var ?PersistentList $ms */
if ($ms === null || !$this->equalizer->equals($s->first(), $ms->first())) {
if (!$ms instanceof self || !$this->equalizer->equals($s->first(), $ms->first())) {
return false;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/php/Lang/Collections/Map/ArrayNode.php
Expand Up @@ -75,7 +75,7 @@ public function remove(int $shift, int $hash, $key): ?HashMapNodeInterface
$index = $this->mask($hash, $shift);
$node = $this->childNodes[$index] ?? null;

if ($node === null) {
if (!$node instanceof HashMapNodeInterface) {
return $this;
}

Expand All @@ -85,7 +85,7 @@ public function remove(int $shift, int $hash, $key): ?HashMapNodeInterface
return $this;
}

if ($n === null) {
if (!$n instanceof HashMapNodeInterface) {
if ($this->count < 8) {
return $this->pack($index);
}
Expand All @@ -107,7 +107,7 @@ public function find(int $shift, int $hash, $key, $notFound)
$index = $this->mask($hash, $shift);
$node = $this->childNodes[$index] ?? null;

if ($node === null) {
if (!$node instanceof HashMapNodeInterface) {
return $notFound;
}

Expand Down Expand Up @@ -135,7 +135,7 @@ private function pack(int $index): HashMapNodeInterface
continue;
}

if ($node === null) {
if (!$node instanceof HashMapNodeInterface) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/php/Lang/Collections/Map/IndexedNode.php
Expand Up @@ -89,7 +89,7 @@ public function remove(int $shift, int $hash, $key): ?HashMapNodeInterface
return $this;
}

if ($n !== null) {
if ($n instanceof HashMapNodeInterface) {
$newObjects = $this->objects;
$newObjects[$index][1] = $n;
return new self($this->hasher, $this->equalizer, $newObjects);
Expand Down
4 changes: 1 addition & 3 deletions src/php/Lang/Collections/Map/PersistentArrayMap.php
Expand Up @@ -136,10 +136,8 @@ public function asTransient(): TransientMapWrapper

/**
* @param K $key
*
* @return int|false
*/
private function findIndex($key): int|bool
private function findIndex($key): int|false
{
for ($i = 0, $cnt = count($this->array); $i < $cnt; $i += 2) {
$k = $this->array[$i];
Expand Down
4 changes: 1 addition & 3 deletions src/php/Lang/Collections/Map/TransientArrayMap.php
Expand Up @@ -130,10 +130,8 @@ public function persistent(): PersistentMapInterface

/**
* @param K $key
*
* @return int|false
*/
private function findIndex($key): int|bool
private function findIndex($key): int|false
{
for ($i = 0, $cnt = count($this->array); $i < $cnt; $i += 2) {
$k = $this->array[$i];
Expand Down
Expand Up @@ -71,7 +71,7 @@ public function remove($key): PersistentMapInterface

public function count(): int
{
return count(static::ALLOWED_KEYS);
return is_countable(static::ALLOWED_KEYS) ? count(static::ALLOWED_KEYS) : 0;
}

public function find($key)
Expand Down
Expand Up @@ -86,7 +86,7 @@ public function equals(mixed $other): bool
for ($s = $this; $s != null; $s = $s->cdr(), $ms = $ms->cdr()) {
/** @var PersistentVectorInterface $s */
/** @var ?PersistentVectorInterface $ms */
if ($ms === null || !$this->equalizer->equals($s->first(), $ms->first())) {
if (!$ms instanceof PersistentVectorInterface || !$this->equalizer->equals($s->first(), $ms->first())) {
return false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/php/Printer/TypePrinter/BooleanPrinter.php
Expand Up @@ -16,7 +16,7 @@ final class BooleanPrinter implements TypePrinterInterface
*/
public function print(mixed $form): string
{
$str = ($form === true) ? 'true' : 'false';
$str = $form ? 'true' : 'false';

return $this->color($str);
}
Expand Down
10 changes: 10 additions & 0 deletions src/php/Printer/TypePrinter/StringPrinter.php
Expand Up @@ -30,6 +30,16 @@ public function __construct(
) {
}

public static function nonReadable(bool $withColor = false): self
{
return new self(readable: false, withColor: $withColor);
}

public static function readable(bool $withColor = false): self
{
return new self(readable: true, withColor: $withColor);
}

public function print(mixed $form): string
{
$str = $this->parseString($form);
Expand Down

0 comments on commit 4fc115b

Please sign in to comment.