Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tighten up types and initialization for private properties and methods. #840

Merged
merged 8 commits into from
Dec 5, 2024
1 change: 0 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ parameters:
- src/Util/Str.php
bootstrapFiles:
- vendor-bin/phpstan/bootstrap.php
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: true
24 changes: 8 additions & 16 deletions src/CodeCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
*/
class CodeCleaner
{
private $yolo = false;
private $strictTypes = false;
private bool $yolo = false;
private bool $strictTypes = false;

private $parser;
private $printer;
private $traverser;
private $namespace;
private Parser $parser;
private Printer $printer;
private NodeTraverser $traverser;
private ?array $namespace = null;

/**
* CodeCleaner constructor.
Expand Down Expand Up @@ -259,8 +259,6 @@ public function clean(array $codeLines, bool $requireSemicolons = false)

/**
* Set the current local namespace.
*
* @param array|null $namespace (default: null)
*/
public function setNamespace(?array $namespace = null)
{
Expand All @@ -285,9 +283,6 @@ public function getNamespace()
* @throws ParseErrorException for parse errors that can't be resolved by
* waiting a line to see what comes next
*
* @param string $code
* @param bool $requireSemicolons
*
* @return array|false A set of statements, or false if incomplete
*/
protected function parse(string $code, bool $requireSemicolons = false)
Expand Down Expand Up @@ -337,9 +332,6 @@ private function parseErrorIsEOF(\PhpParser\Error $e): bool
* Unlike (all?) other unclosed statements, single quoted strings have
* their own special beautiful snowflake syntax error just for
* themselves.
*
* @param \PhpParser\Error $e
* @param string $code
*/
private function parseErrorIsUnclosedString(\PhpParser\Error $e, string $code): bool
{
Expand All @@ -356,12 +348,12 @@ private function parseErrorIsUnclosedString(\PhpParser\Error $e, string $code):
return true;
}

private function parseErrorIsUnterminatedComment(\PhpParser\Error $e, $code): bool
private function parseErrorIsUnterminatedComment(\PhpParser\Error $e, string $code): bool
{
return $e->getRawMessage() === 'Unterminated comment';
}

private function parseErrorIsTrailingComma(\PhpParser\Error $e, $code): bool
private function parseErrorIsTrailingComma(\PhpParser\Error $e, string $code): bool
{
return ($e->getRawMessage() === 'A trailing comma is not allowed here') && (\substr(\rtrim($code), -1) === ',');
}
Expand Down
4 changes: 2 additions & 2 deletions src/CodeCleaner/AbstractClassPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
class AbstractClassPass extends CodeCleanerPass
{
private $class;
private $abstractMethods;
private Class_ $class;
private array $abstractMethods;

/**
* @throws FatalErrorException if the node is an abstract function with a body
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/CalledClassPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class CalledClassPass extends CodeCleanerPass
{
private $inClass;
private bool $inClass = false;

/**
* @param array $nodes
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/EmptyArrayDimFetchPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class EmptyArrayDimFetchPass extends CodeCleanerPass
{
const EXCEPTION_MESSAGE = 'Cannot use [] for reading';

private $theseOnesAreFine = [];
private array $theseOnesAreFine = [];

/**
* @return Node[]|null Array of nodes
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/FinalClassPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class FinalClassPass extends CodeCleanerPass
{
private $finalClasses;
private array $finalClasses = [];

/**
* @param array $nodes
Expand Down
3 changes: 1 addition & 2 deletions src/CodeCleaner/FunctionContextPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

class FunctionContextPass extends CodeCleanerPass
{
/** @var int */
private $functionDepth;
private int $functionDepth = 0;

/**
* @param array $nodes
Expand Down
10 changes: 3 additions & 7 deletions src/CodeCleaner/LabelContextPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@
*/
class LabelContextPass extends CodeCleanerPass
{
/** @var int */
private $functionDepth;

/** @var array */
private $labelDeclarations;
/** @var array */
private $labelGotos;
private int $functionDepth = 0;
private array $labelDeclarations = [];
private array $labelGotos = [];

/**
* @param array $nodes
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/LoopContextPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class LoopContextPass extends CodeCleanerPass
{
private $loopDepth;
private int $loopDepth = 0;

/**
* {@inheritdoc}
Expand Down
4 changes: 2 additions & 2 deletions src/CodeCleaner/NamespaceAwarePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
abstract class NamespaceAwarePass extends CodeCleanerPass
{
protected $namespace;
protected $currentScope;
protected array $namespace = [];
protected array $currentScope = [];

/**
* @todo should this be final? Extending classes should be sure to either
Expand Down
6 changes: 3 additions & 3 deletions src/CodeCleaner/NamespacePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
*/
class NamespacePass extends CodeCleanerPass
{
private $namespace = null;
private $cleaner;
private ?Name $namespace = null;
private CodeCleaner $cleaner;

/**
* @param CodeCleaner $cleaner
Expand Down Expand Up @@ -83,7 +83,7 @@ public function beforeTraverse(array $nodes)
*
* @param Name|null $namespace
*/
private function setNamespace($namespace)
private function setNamespace(?Name $namespace)
{
$this->namespace = $namespace;
$this->cleaner->setNamespace($namespace === null ? null : $this->getParts($namespace));
Expand Down
4 changes: 2 additions & 2 deletions src/CodeCleaner/RequirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class RequirePass extends CodeCleanerPass
{
private static $requireTypes = [Include_::TYPE_REQUIRE, Include_::TYPE_REQUIRE_ONCE];
private const REQUIRE_TYPES = [Include_::TYPE_REQUIRE, Include_::TYPE_REQUIRE_ONCE];

/**
* {@inheritdoc}
Expand Down Expand Up @@ -125,7 +125,7 @@ public static function resolve($file, $startLine = null): string

private function isRequireNode(Node $node): bool
{
return $node instanceof Include_ && \in_array($node->type, self::$requireTypes);
return $node instanceof Include_ && \in_array($node->type, self::REQUIRE_TYPES);
}

private static function getIncludePath(): array
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/ReturnTypePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ReturnTypePass extends CodeCleanerPass
const VOID_NULL_MESSAGE = 'A void function must not return a value (did you mean "return;" instead of "return null;"?)';
const NULLABLE_VOID_MESSAGE = 'Void type cannot be nullable';

private $returnTypeStack = [];
private array $returnTypeStack = [];

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/StrictTypesPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class StrictTypesPass extends CodeCleanerPass
{
const EXCEPTION_MESSAGE = 'strict_types declaration must have 0 or 1 as its value';

private $strictTypes = false;
private bool $strictTypes;

/**
* @param bool $strictTypes enforce strict types by default
Expand Down
6 changes: 3 additions & 3 deletions src/CodeCleaner/UseStatementPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
*/
class UseStatementPass extends CodeCleanerPass
{
private $aliases = [];
private $lastAliases = [];
private $lastNamespace = null;
private array $aliases = [];
private array $lastAliases = [];
private ?Name $lastNamespace = null;

/**
* Re-load the last set of use statements on re-entering a namespace.
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/ValidClassNamePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ValidClassNamePass extends NamespaceAwarePass
const INTERFACE_TYPE = 'interface';
const TRAIT_TYPE = 'trait';

private $conditionalScopes = 0;
private int $conditionalScopes = 0;

/**
* Validate class, interface and trait definitions.
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/ValidConstructorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
class ValidConstructorPass extends CodeCleanerPass
{
private $namespace;
private array $namespace = [];

/**
* @return Node[]|null Array of nodes
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCleaner/ValidFunctionNamePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
class ValidFunctionNamePass extends NamespaceAwarePass
{
private $conditionalScopes = 0;
private int $conditionalScopes = 0;

/**
* Store newly defined function names on the way in, to allow recursion.
Expand Down
10 changes: 3 additions & 7 deletions src/Command/BufferCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Psy\Command;

use Psy\Exception\RuntimeException;
use Psy\Output\ShellOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -52,14 +51,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$app = $this->getApplication();
if (!$app instanceof \Psy\Shell) {
throw new RuntimeException('Buffer command requires a \Psy\Shell application');
}
$shell = $this->getShell();

$buf = $app->getCodeBuffer();
$buf = $shell->getCodeBuffer();
if ($input->getOption('clear')) {
$app->resetCodeBuffer();
$shell->resetCodeBuffer();
$output->writeln($this->formatLines($buf, 'urgent'), ShellOutput::NUMBER_LINES);
} else {
$output->writeln($this->formatLines($buf), ShellOutput::NUMBER_LINES);
Expand Down
2 changes: 1 addition & 1 deletion src/Command/CodeArgumentParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class CodeArgumentParser
{
private $parser;
private Parser $parser;

public function __construct(?Parser $parser = null)
{
Expand Down
13 changes: 13 additions & 0 deletions src/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ public function setApplication(?Application $application = null): void
parent::setApplication($application);
}

/**
* getApplication, but is guaranteed to return a Shell instance.
*/
protected function getShell(): Shell
{
$shell = $this->getApplication();
if (!$shell instanceof Shell) {
throw new \RuntimeException('PsySH Commands require an instance of Psy\Shell');
}

return $shell;
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Command/DocCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$doc = $this->getManualDoc($reflector) ?: DocblockFormatter::format($reflector);
}

$db = $this->getApplication()->getManualDb();
$db = $this->getShell()->getManualDb();

if ($output instanceof ShellOutput) {
$output->startPaging();
Expand Down Expand Up @@ -242,7 +242,7 @@ private function getParentReflectors($reflector): \Generator

private function getManualDocById($id)
{
if ($db = $this->getApplication()->getManualDb()) {
if ($db = $this->getShell()->getManualDb()) {
$result = $db->query(\sprintf('SELECT doc FROM php_manual WHERE id = %s', $db->quote($id)));
if ($result !== false) {
return $result->fetchColumn(0);
Expand Down
8 changes: 7 additions & 1 deletion src/Command/DumpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Psy\Command;

use Psy\Exception\RuntimeException;
use Psy\Input\CodeArgument;
use Psy\Output\ShellOutput;
use Psy\VarDumper\Presenter;
use Psy\VarDumper\PresenterAware;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -25,7 +27,7 @@
*/
class DumpCommand extends ReflectingCommand implements PresenterAware
{
private $presenter;
private Presenter $presenter;

/**
* PresenterAware interface.
Expand Down Expand Up @@ -71,6 +73,10 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$output instanceof ShellOutput) {
throw new RuntimeException('DumpCommand requires a ShellOutput');
}

$depth = $input->getOption('depth');
$target = $this->resolveCode($input->getArgument('target'));
$output->page($this->presenter->present($target, $depth, $input->getOption('all') ? Presenter::VERBOSE : 0));
Expand Down
13 changes: 3 additions & 10 deletions src/Command/EditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,8 @@

class EditCommand extends Command implements ContextAware
{
/**
* @var string
*/
private $runtimeDir = '';

/**
* @var Context
*/
private $context;
private string $runtimeDir = '';
private Context $context;

/**
* Constructor.
Expand Down Expand Up @@ -104,7 +97,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$editedContent = $this->editFile($filePath, $shouldRemoveFile);

if ($execute) {
$this->getApplication()->addInput($editedContent);
$this->getShell()->addInput($editedContent);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/Command/HelpCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
class HelpCommand extends Command
{
private $command;
private ?Command $command = null;

/**
* {@inheritdoc}
Expand Down
Loading
Loading