Skip to content

[TASK] Use native type declarations in OutputFormatter #964

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

Merged
merged 1 commit into from
Feb 20, 2025
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Please also have a look at our
- Only allow `string` for some `OutputFormat` properties (#885)
- Make all non-private properties `@internal` (#886)
- Use more native type declarations and strict mode
(#641, #772, #774, #778, #804, #841, #873, #875, #891, #922, #923, #933)
(#641, #772, #774, #778, #804, #841, #873, #875, #891, #922, #923, #933, #958,
#964)
Comment on lines +29 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for adding #958.

- Add visibility to all class/interface constants (#469)

### Deprecated
Expand Down
6 changes: 0 additions & 6 deletions config/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ parameters:
count: 4
path: ../src/OutputFormat.php

-
message: '#^Parameters should have "string" types as the only types passed to this method$#'
identifier: typePerfect.narrowPublicClassMethodParamType
count: 3
path: ../src/OutputFormatter.php

-
message: '#^Default value of the parameter \#2 \$bIncludeEnd \(false\) of method Sabberworm\\CSS\\Parsing\\ParserState\:\:consumeUntil\(\) is incompatible with type string\.$#'
identifier: parameter.defaultValue
Expand Down
1 change: 1 addition & 0 deletions src/OutputFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ public function set($aNames, $mValue)
}

/**
* @param non-empty-string $sMethodName
* @param array<array-key, mixed> $aArguments
*
* @return mixed
Expand Down
39 changes: 8 additions & 31 deletions src/OutputFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ public function __construct(OutputFormat $outputFormat)
$this->outputFormat = $outputFormat;
}

/**
* @param string $sName
* @param string|null $sType
*/
public function space($sName, $sType = null): string
public function space(string $sName, ?string $sType = null): string
{
$sSpaceString = $this->outputFormat->get("Space$sName");
// If $sSpaceString is an array, we have multiple values configured
Expand Down Expand Up @@ -78,9 +74,6 @@ public function spaceBeforeSelectorSeparator(): string
return $this->space('BeforeSelectorSeparator');
}

/**
* @return string
*/
public function spaceAfterSelectorSeparator(): string
{
return $this->space('AfterSelectorSeparator');
Expand All @@ -89,7 +82,7 @@ public function spaceAfterSelectorSeparator(): string
/**
* @param non-empty-string $sSeparator
*/
public function spaceBeforeListArgumentSeparator($sSeparator): string
public function spaceBeforeListArgumentSeparator(string $sSeparator): string
{
$spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators();

Expand All @@ -99,7 +92,7 @@ public function spaceBeforeListArgumentSeparator($sSeparator): string
/**
* @param non-empty-string $sSeparator
*/
public function spaceAfterListArgumentSeparator($sSeparator): string
public function spaceAfterListArgumentSeparator(string $sSeparator): string
{
$spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators();

Expand All @@ -112,13 +105,9 @@ public function spaceBeforeOpeningBrace(): string
}

/**
* Runs the given code, either swallowing or passing exceptions, depending on the `bIgnoreExceptions` setting.
*
* @param string $cCode the name of the function to call
*
* @return string|null
* Runs the given code, either swallowing or passing exceptions, depending on the `ignoreExceptions` setting.
*/
public function safely($cCode)
public function safely(callable $cCode): ?string
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍 callable appears to be the correct parameter type, rather than string.

{
if ($this->outputFormat->get('IgnoreExceptions')) {
// If output exceptions are ignored, run the code with exception guards
Expand All @@ -137,9 +126,8 @@ public function safely($cCode)
* Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
*
* @param array<array-key, Renderable|string> $aValues
* @param bool $bIncreaseLevel
*/
public function implode(string $sSeparator, array $aValues, $bIncreaseLevel = false): string
public function implode(string $sSeparator, array $aValues, bool $bIncreaseLevel = false): string
{
$result = '';
$outputFormat = $this->outputFormat;
Expand All @@ -162,12 +150,7 @@ public function implode(string $sSeparator, array $aValues, $bIncreaseLevel = fa
return $result;
}

/**
* @param string $sString
*
* @return string
*/
public function removeLastSemicolon($sString)
public function removeLastSemicolon(string $sString): string
{
if ($this->outputFormat->get('SemicolonAfterLastRule')) {
return $sString;
Expand Down Expand Up @@ -199,17 +182,11 @@ public function comments(Commentable $oCommentable): string
return $result;
}

/**
* @param string $sSpaceString
*/
private function prepareSpace($sSpaceString): string
private function prepareSpace(string $sSpaceString): string
{
return \str_replace("\n", "\n" . $this->indent(), $sSpaceString);
}

/**
* @return string
*/
private function indent(): string
{
return \str_repeat($this->outputFormat->sIndentation, $this->outputFormat->getIndentationLevel());
Expand Down