Skip to content

Add initial PHPStan configuration#201

Merged
swissspidy merged 14 commits intomainfrom
fix/phpstan
Apr 8, 2026
Merged

Add initial PHPStan configuration#201
swissspidy merged 14 commits intomainfrom
fix/phpstan

Conversation

@swissspidy
Copy link
Copy Markdown
Member

No description provided.

@github-actions

This comment was marked as resolved.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 2, 2026

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the codebase's type safety and static analysis compatibility by adding comprehensive PHPDoc annotations, implementing variadic parameters for output functions, and introducing a PHPStan configuration. Key refactorings include updating the lexer logic in Arguments.php, ensuring consistent return types across various classes, and adding explicit type casting for environment variables and calculations. The review feedback highlights a few remaining inconsistencies, specifically regarding indentation and method usage in Arguments.php, and a missing variadic parameter in the line() function to match the updated out() and err() signatures.

@swissspidy swissspidy added this to the 0.13.0 milestone Apr 7, 2026
@swissspidy swissspidy marked this pull request as ready for review April 7, 2026 09:51
@swissspidy swissspidy requested a review from a team as a code owner April 7, 2026 09:51
Copilot AI review requested due to automatic review settings April 7, 2026 09:51
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces an initial PHPStan setup for the project and updates a broad set of library files with more precise PHPDoc/typing and small refactors to make the codebase pass stricter static analysis.

Changes:

  • Add phpstan.neon.dist with level 9 configuration and WordPress/WP-CLI stubs scanning.
  • Tighten PHPDoc types across CLI components (tables, trees, streams, arguments, notify/progress).
  • Refactor a few implementations for safer scalar casting and iterator handling (notably arguments parsing and stream rendering).

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
phpstan.neon.dist Adds baseline PHPStan configuration (level, paths, stubs).
lib/cli/tree/Renderer.php Narrows $tree PHPDoc type for static analysis.
lib/cli/tree/Markdown.php Narrows $tree PHPDoc type for static analysis.
lib/cli/tree/Ascii.php Narrows $tree PHPDoc type for static analysis.
lib/cli/Tree.php Adds property/param/return PHPDoc types for PHPStan.
lib/cli/table/Tabular.php Adds stricter row typing and safer string conversions.
lib/cli/table/Renderer.php Adds property/param typing and return annotations.
lib/cli/table/Ascii.php Adds property typing and multiple safety casts/guards.
lib/cli/Table.php Adds property typing; constructor refactor to sanitize inputs.
lib/cli/Streams.php Adds typing/docs; refactors render/out/err to variadics and safer casting.
lib/cli/Shell.php Minor robustness tweak and adds return annotation.
lib/cli/progress/Bar.php Adds property typing; tightens numeric-to-string conversions.
lib/cli/Progress.php Adds typing/docs; small return type normalization.
lib/cli/notify/Spinner.php Adds property typing/docs.
lib/cli/notify/Dots.php Adds property typing; initializes iteration counter.
lib/cli/Notify.php Adds property typing/docs; adjusts speed/time formatting.
lib/cli/Memoize.php Adds typing/docs; refactors memoized getter invocation.
lib/cli/Colors.php Adds typing/docs; adds guards/asserts for split/substr results.
lib/cli/cli.php Updates function signatures/docs; adds type guards and stronger checks.
lib/cli/arguments/Lexer.php Adds typing/docs; makes shifting/exploding safer for null cases.
lib/cli/arguments/InvalidArguments.php Adds typing/docs for stored invalid arguments.
lib/cli/arguments/HelpScreen.php Adds typing/docs; adds scalar-safe formatting of descriptions/defaults.
lib/cli/arguments/Argument.php Adds property-read PHPDoc and stronger typing in exploded().
lib/cli/Arguments.php Adds typing/docs; improves input normalization and option parsing iteration.
Comments suppressed due to low confidence (2)

lib/cli/Arguments.php:589

  • The "no value given" check intends to allow a default value of 0, but the condition currently compares the entire $optionSettings array to 0 ($optionSettings !== 0), which is always true. As a result, options with default 0 will incorrectly warn and be set to null. Compare against $optionSettings['default'] instead (or explicitly handle array_key_exists('default', ...)).
		if ($this->_lexer->end() || !$this->_lexer->peek->isValue) {
			$optionSettings = $this->getOption($option->key);

			if (empty($optionSettings['default']) && $optionSettings !== 0) {
				// Oops! Got no value and no default , throw a warning and continue.
				$this->_warn('no value given for ' . $option->raw);
				$this[$option->key] = null;
			} else {
				// No value and we have a default, so we set to the default
				$this[$option->key] = $optionSettings['default'];
			}

lib/cli/cli.php:349

  • grapheme_strlen() returns int|false on failure (not null). The current check null !== ( $width = grapheme_strlen(...) ) will treat false as a valid width and return early (coercing false to 0), skipping the safer fallbacks. Use an is_int(...)/false !== ... check consistent with safe_strlen() to avoid mis-measuring non‑UTF8 strings.
	// Assume UTF-8 if no encoding given - `grapheme_strlen()` will return null if given non-UTF-8 string.
	if ( ( ! $encoding || 'UTF-8' === $encoding ) && can_use_icu() && null !== ( $width = grapheme_strlen( $string ) ) ) {
		if ( ! $test_strwidth || ( $test_strwidth & 1 ) ) {
			return $width + preg_match_all( $eaw_regex, $string, $dummy /*needed for PHP 5.3*/ );
		}
	}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@swissspidy

This comment was marked as resolved.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances type safety and robustness across the library by adding comprehensive PHPDoc type hints, implementing stricter input validation, and introducing a PHPStan configuration. Key updates include the use of variadic arguments in the Streams class and cli.php, along with improved handling of scalar and object-to-string conversions in table and menu components. Feedback was provided concerning the explicit integer casting in Notify::speed() and Progress::estimated(), which results in a loss of precision for calculated speed and time estimates.

@swissspidy swissspidy merged commit 4a04ffb into main Apr 8, 2026
23 checks passed
@swissspidy swissspidy deleted the fix/phpstan branch April 8, 2026 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants