Skip to content

Commit

Permalink
Merge branch 'reduce-memory-usage'
Browse files Browse the repository at this point in the history
  • Loading branch information
lkrms committed Mar 7, 2024
2 parents ba9f275 + b2e8ae2 commit f958f24
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 91 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
key: composer-cache-${{ runner.os }}-${{ hashFiles('**/composer.json', '**/composer.lock') }}
restore-keys: composer-cache-${{ runner.os }}-

- name: Install dependencies
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress --no-dev

- name: Run PHP CS Fixer
Expand Down Expand Up @@ -92,7 +92,7 @@ jobs:
key: phpstan-cache-${{ runner.os }}-${{ matrix.php-version }}-${{ github.run_id }}
restore-keys: phpstan-cache-${{ runner.os }}-${{ matrix.php-version }}-

- name: Install dependencies
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress

- name: Run PHPStan
Expand Down Expand Up @@ -145,7 +145,7 @@ jobs:
key: composer-cache-${{ runner.os }}-${{ hashFiles('**/composer.json', '**/composer.lock') }}
restore-keys: composer-cache-${{ runner.os }}-

- name: Install dependencies
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress

- name: Run PHPUnit tests and generate code coverage report
Expand Down Expand Up @@ -193,7 +193,7 @@ jobs:
sudo apt-get update
sudo apt-get install pandoc
- name: Install dependencies
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress

# Run the phar and man builds separately and together to test each code
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"ext-mbstring": "*",
"ext-tokenizer": "*",
"composer-runtime-api": "^2.2",
"salient/toolkit": "^0.99.0",
"salient/toolkit": "^0.99.10",
"sebastian/diff": "^4 || ^5"
},
"autoload": {
Expand Down
15 changes: 9 additions & 6 deletions composer.lock

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

16 changes: 8 additions & 8 deletions src/Command/FormatPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@
use Lkrms\PrettyPHP\Token\Token;
use Lkrms\PrettyPHP\Formatter;
use Lkrms\PrettyPHP\FormatterBuilder;
use Salient\Cli\Catalog\CliHelpSectionName;
use Salient\Cli\Catalog\CliOptionType;
use Salient\Cli\Catalog\CliOptionValueType;
use Salient\Cli\Catalog\CliOptionValueUnknownPolicy;
use Salient\Cli\Catalog\CliOptionVisibility as Visibility;
use Salient\Cli\Exception\CliInvalidArgumentsException;
use Salient\Cli\CliCommand;
use Salient\Cli\CliOption;
use Salient\Console\Catalog\ConsoleLevel;
use Salient\Contract\Cli\CliHelpSectionName;
use Salient\Contract\Cli\CliOptionType;
use Salient\Contract\Cli\CliOptionValueType;
use Salient\Contract\Cli\CliOptionValueUnknownPolicy;
use Salient\Contract\Cli\CliOptionVisibility as Visibility;
use Salient\Contract\Core\MessageLevel as Level;
use Salient\Core\Facade\Console;
use Salient\Core\Facade\Profile;
use Salient\Core\Utility\Arr;
Expand Down Expand Up @@ -778,7 +778,7 @@ protected function run(...$params)
}

if ($this->ReportTimers) {
$this->App->registerShutdownReport(ConsoleLevel::NOTICE);
$this->App->registerShutdownReport(Level::NOTICE);
}

if ($this->Tabs && $this->Spaces) {
Expand Down Expand Up @@ -1176,7 +1176,7 @@ protected function run(...$params)
if ($this->Diff) {
if ($this->Quiet < 2) {
if ($replaced) {
Console::out('', ConsoleLevel::INFO);
Console::out('', Level::INFO);
}
Console::log(Inflect::format(
$count,
Expand Down
8 changes: 5 additions & 3 deletions src/Contract/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace Lkrms\PrettyPHP\Contract;

use Lkrms\PrettyPHP\Token\Token;
use PhpToken;

interface Filter extends Extension
{
/**
* Apply the filter to an array of tokens
*
* @param Token[] $tokens
* @return Token[]
* @template T of PhpToken
*
* @param T[] $tokens
* @return T[]
*/
public function filterTokens(array $tokens): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Thrown when incompatible rules are enabled
*/
class EnabledRulesAreNotCompatible extends AbstractException
class IncompatibleRulesException extends AbstractException
{
/**
* @var array<class-string<Rule>>
Expand Down
14 changes: 12 additions & 2 deletions src/Filter/CollectColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

namespace Lkrms\PrettyPHP\Filter;

use Lkrms\PrettyPHP\Concern\ExtensionTrait;
use Lkrms\PrettyPHP\Contract\Filter;
use Lkrms\PrettyPHP\Filter\Concern\FilterTrait;
use Lkrms\PrettyPHP\Token\Token;
use Salient\Core\Utility\Str;

/**
* Assign the starting column of each token to its $column property
*/
final class CollectColumn implements Filter
{
use ExtensionTrait;
use FilterTrait;

/**
* @inheritDoc
*/
public function filterTokens(array $tokens): array
{
if (!$this->isArrayOf($tokens, Token::class)) {
// @codeCoverageIgnoreStart
return $tokens;
// @codeCoverageIgnoreEnd
}

$tokens = array_values($tokens);
$last = array_key_last($tokens);
$column = 1;
Expand Down
20 changes: 17 additions & 3 deletions src/Filter/Concern/FilterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,35 @@
namespace Lkrms\PrettyPHP\Filter\Concern;

use Lkrms\PrettyPHP\Concern\ExtensionTrait;
use Lkrms\PrettyPHP\Token\Token;
use PhpToken;

trait FilterTrait
{
use ExtensionTrait;

/**
* @var Token[]
* @var PhpToken[]
*/
protected array $Tokens;

/**
* Check if the given tokens are instances of the given class
*
* @template T of PhpToken
*
* @param PhpToken[] $tokens
* @param class-string<T> $class
* @phpstan-assert-if-true T[] $tokens
*/
protected function isArrayOf(array $tokens, string $class): bool
{
return $tokens && reset($tokens) instanceof $class;
}

/**
* Get the given token's previous code token
*/
protected function prevCode(int $i): ?Token
protected function prevCode(int $i): ?PhpToken
{
while ($i--) {
$token = $this->Tokens[$i];
Expand Down
18 changes: 15 additions & 3 deletions src/Filter/RemoveHeredocIndentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Lkrms\PrettyPHP\Contract\Filter;
use Lkrms\PrettyPHP\Token\Token;
use Salient\Core\Utility\Pcre;
use PhpToken;

/**
* Remove indentation from heredocs
Expand All @@ -14,13 +15,19 @@ final class RemoveHeredocIndentation implements Filter
{
use ExtensionTrait;

/**
* @template T of PhpToken
*
* @param T[] $tokens
* @return T[]
*/
public function filterTokens(array $tokens): array
{
/** @var array<int,Token[]> */
/** @var array<int,T[]> */
$heredocTokens = [];
/** @var array<int,string[]> */
$heredocText = [];
/** @var array<int,Token> */
/** @var array<int,T> */
$stack = [];
foreach ($tokens as $i => $token) {
if ($stack) {
Expand Down Expand Up @@ -73,7 +80,12 @@ public function filterTokens(array $tokens): array

// Finally, update each token
foreach ($stripped as $j => $code) {
$heredocTokens[$i][$j]->setText($code);
$token = $heredocTokens[$i][$j];
if ($token instanceof Token) {
$token->setText($code);
} else {
$token->text = $code;
}
}
}

Expand Down
27 changes: 17 additions & 10 deletions src/Filter/SortImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Lkrms\PrettyPHP\Catalog\ImportSortOrder;
use Lkrms\PrettyPHP\Contract\Filter;
use Lkrms\PrettyPHP\Filter\Concern\FilterTrait;
use Lkrms\PrettyPHP\Token\Token;
use Salient\Core\Utility\Arr;
use Salient\Core\Utility\Pcre;
use PhpToken;

/**
* Sort consecutive alias/import statements
Expand All @@ -34,7 +34,10 @@ final class SortImports implements Filter
private array $SortableImports;

/**
* @inheritDoc
* @template T of PhpToken
*
* @param T[] $tokens
* @return T[]
*/
public function filterTokens(array $tokens): array
{
Expand All @@ -44,7 +47,7 @@ public function filterTokens(array $tokens): array

// Identify relevant `T_USE` tokens and exit early if possible
$tokens = [];
/** @var array<int,Token> */
/** @var array<int,T> */
$stack = [];
foreach ($this->Tokens as $i => $token) {
if ($this->TypeIndex->OpenBrace[$token->id]) {
Expand Down Expand Up @@ -79,11 +82,11 @@ public function filterTokens(array $tokens): array
while ($tokens) {
$i = array_shift($tokens);

/** @var array<non-empty-array<Token>> */
/** @var array<non-empty-array<T>> */
$sort = [];
/** @var Token[] */
/** @var T[] */
$current = [];
/** @var Token|null */
/** @var T|null */
$terminator = null;
while ($i < $count) {
$token = $this->Tokens[$i];
Expand Down Expand Up @@ -145,9 +148,11 @@ public function filterTokens(array $tokens): array
}

/**
* @param non-empty-array<non-empty-array<Token>> $sort
* @param Token[] $tokens
* @return Token[]
* @template T of PhpToken
*
* @param non-empty-array<non-empty-array<T>> $sort
* @param T[] $tokens
* @return T[]
*/
private function sortImports(array $sort, array $tokens): array
{
Expand Down Expand Up @@ -190,7 +195,9 @@ function (array $a, array $b): int {
}

/**
* @param Token[] $tokens
* @template T of PhpToken
*
* @param T[] $tokens
* @return array{int,string}
*/
private function sortableImport(array $tokens): array
Expand Down
8 changes: 7 additions & 1 deletion src/Filter/TrimCasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Lkrms\PrettyPHP\Concern\ExtensionTrait;
use Lkrms\PrettyPHP\Contract\Filter;
use Lkrms\PrettyPHP\Token\Token;

/**
* Remove whitespace inside cast operators
Expand All @@ -24,7 +25,12 @@ public function filterTokens(array $tokens): array
$token->id === \T_ARRAY_CAST ||
$token->id === \T_OBJECT_CAST ||
$token->id === \T_UNSET_CAST) {
$token->setText('(' . trim($token->text, " \n\r\t\v\0()") . ')');
$text = '(' . trim($token->text, " \n\r\t\v\0()") . ')';
if ($token instanceof Token) {
$token->setText($text);
} else {
$token->text = $text;
}
}
}

Expand Down
Loading

0 comments on commit f958f24

Please sign in to comment.