Skip to content

Commit

Permalink
feat: adds php 8.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Oct 15, 2024
1 parent 642f96d commit 20122c2
Show file tree
Hide file tree
Showing 27 changed files with 50 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
dependency-version: [prefer-lowest, prefer-stable]
dependency-version: [prefer-stable]

name: PHPStan ${{ matrix.dependency-version }}

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3']
dependency-version: [prefer-lowest, prefer-stable]
php: ['8.1', '8.2', '8.3', '8.4']
dependency-version: [prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.parallel }}

Expand Down
21 changes: 10 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-mbstring": "*",
"symfony/console": "^5.3.0|^6.0.0"
"symfony/console": "^6.4.12"
},
"require-dev": {
"ergebnis/phpstan-rules": "^1.0.",
"illuminate/console": "^8.0|^9.0|^10.0",
"illuminate/support": "^8.0|^9.0|^10.0",
"laravel/pint": "^1.0.0",
"pestphp/pest": "^1.21.0",
"pestphp/pest-plugin-mock": "^1.0",
"phpstan/phpstan": "^1.4.6",
"phpstan/phpstan-strict-rules": "^1.1.0",
"symfony/var-dumper": "^5.2.7|^6.0.0",
"illuminate/console": "^10.48.22",
"illuminate/support": "^10.48.22",
"laravel/pint": "^1.18.1",
"pestphp/pest": "^2",
"pestphp/pest-plugin-mock": "2.0.0",
"phpstan/phpstan": "^1.12.6",
"phpstan/phpstan-strict-rules": "^1.6.1",
"symfony/var-dumper": "^6.4.11",
"thecodingmachine/phpstan-strict-rules": "^1.0.0"
},
"autoload": {
Expand Down
8 changes: 0 additions & 8 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/ergebnis/phpstan-rules/rules.neon
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon

parameters:
Expand All @@ -11,10 +10,3 @@ parameters:
checkMissingIterableValueType: true
checkGenericClassInNonGenericObjectType: true
reportUnmatchedIgnoredErrors: true

ignoreErrors:
- '#is not allowed to extend#'
- '#with default value.#'
- '#Variable method call on#'
- '#as default value#'
- '#is not final, but since the containing class is abstract, it should be#'
1 change: 1 addition & 0 deletions src/Actions/StyleToMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function __invoke(string|int ...$arguments): Styles
return $this->__invoke(...$arguments);
}

// @phpstan-ignore-next-line
return $this->styles
->setStyle($this->style)
->$methodName(...array_reverse($arguments));
Expand Down
4 changes: 1 addition & 3 deletions src/Components/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

namespace Termwind\Components;

final class Anchor extends Element
{
}
final class Anchor extends Element {}
3 changes: 2 additions & 1 deletion src/Components/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final public static function fromStyles(OutputInterface $output, array|string $c
public function toString(): string
{
if (is_array($this->content)) {
$inheritance = new InheritStyles();
$inheritance = new InheritStyles;
$this->content = implode('', $inheritance($this->content, $this->styles));
}

Expand All @@ -83,6 +83,7 @@ public function toString(): string
public function __call(string $name, array $arguments): mixed
{
if (method_exists($this->styles, $name)) {
// @phpstan-ignore-next-line
$result = $this->styles->{$name}(...$arguments);

if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/ColorNotFound.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
/**
* @internal
*/
final class ColorNotFound extends InvalidArgumentException
{
}
final class ColorNotFound extends InvalidArgumentException {}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidChild.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
/**
* @internal
*/
final class InvalidChild extends InvalidArgumentException
{
}
final class InvalidChild extends InvalidArgumentException {}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
/**
* @internal
*/
final class InvalidColor extends InvalidArgumentException
{
}
final class InvalidColor extends InvalidArgumentException {}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@
/**
* @internal
*/
final class InvalidStyle extends InvalidArgumentException
{
}
final class InvalidStyle extends InvalidArgumentException {}
6 changes: 3 additions & 3 deletions src/Functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Sets the renderer implementation.
*/
function renderUsing(OutputInterface|null $renderer): void
function renderUsing(?OutputInterface $renderer): void
{
Termwind::renderUsing($renderer);
}
Expand All @@ -26,7 +26,7 @@ function renderUsing(OutputInterface|null $renderer): void
*
* @param (Closure(Styles $renderable, string|int ...$arguments): Styles)|null $callback
*/
function style(string $name, Closure $callback = null): Style
function style(string $name, ?Closure $callback = null): Style
{
return StyleRepository::create($name, $callback);
}
Expand Down Expand Up @@ -58,7 +58,7 @@ function terminal(): Terminal
*
* @param iterable<array-key, string>|null $autocomplete
*/
function ask(string $question, iterable $autocomplete = null): mixed
function ask(string $question, ?iterable $autocomplete = null): mixed
{
return (new Question)->ask($question, $autocomplete);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Html/TableRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function parseFoot(Node $node): void
if ($child->isName('tr')) {
$rows = iterator_to_array($this->parseRow($child));
if (count($rows) > 0) {
$this->table->addRow(new TableSeparator());
$this->table->addRow(new TableSeparator);
$this->table->addRows($rows);
}
}
Expand Down Expand Up @@ -203,7 +203,7 @@ private function parseRow(Node $node): Iterator

$border = (int) $node->getAttribute('border');
for ($i = $border; $i--; $i > 0) {
yield new TableSeparator();
yield new TableSeparator;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function render(string $html, int $options): void
*/
public function parse(string $html): Components\Element
{
$dom = new DOMDocument();
$dom = new DOMDocument;

if (strip_tags($html) === $html) {
return Termwind::span($html);
Expand Down
12 changes: 6 additions & 6 deletions src/Question.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,33 @@ final class Question
*/
private SymfonyQuestionHelper $helper;

public function __construct(SymfonyQuestionHelper $helper = null)
public function __construct(?SymfonyQuestionHelper $helper = null)
{
$this->helper = $helper ?? new QuestionHelper();
$this->helper = $helper ?? new QuestionHelper;
}

/**
* Sets the streamable input implementation.
*/
public static function setStreamableInput(StreamableInputInterface|null $streamableInput): void
{
self::$streamableInput = $streamableInput ?? new ArgvInput();
self::$streamableInput = $streamableInput ?? new ArgvInput;
}

/**
* Gets the streamable input implementation.
*/
public static function getStreamableInput(): StreamableInputInterface
{
return self::$streamableInput ??= new ArgvInput();
return self::$streamableInput ??= new ArgvInput;
}

/**
* Renders a prompt to the user.
*
* @param iterable<array-key, string>|null $autocomplete
*/
public function ask(string $question, iterable $autocomplete = null): mixed
public function ask(string $question, ?iterable $autocomplete = null): mixed
{
$html = (new HtmlRenderer)->parse($question)->toString();

Expand All @@ -73,7 +73,7 @@ public function ask(string $question, iterable $autocomplete = null): mixed

$currentHelper = $property->isInitialized($output)
? $property->getValue($output)
: new SymfonyQuestionHelper();
: new SymfonyQuestionHelper;

$property->setValue($output, new QuestionHelper);

Expand Down
2 changes: 1 addition & 1 deletion src/Repositories/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class Styles
*
* @param (Closure(StylesValueObject $element, string|int ...$arguments): StylesValueObject)|null $callback
*/
public static function create(string $name, Closure $callback = null): Style
public static function create(string $name, ?Closure $callback = null): Style
{
self::$storage[$name] = $style = new Style(
$callback ?? static fn (StylesValueObject $styles) => $styles
Expand Down
4 changes: 2 additions & 2 deletions src/Terminal.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ final class Terminal
/**
* Creates a new terminal instance.
*/
public function __construct(ConsoleTerminal $terminal = null)
public function __construct(?ConsoleTerminal $terminal = null)
{
$this->terminal = $terminal ?? new ConsoleTerminal();
$this->terminal = $terminal ?? new ConsoleTerminal;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Termwind.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class Termwind
*/
public static function renderUsing(OutputInterface|null $renderer): void
{
self::$renderer = $renderer ?? new ConsoleOutput();
self::$renderer = $renderer ?? new ConsoleOutput;
}

/**
Expand Down Expand Up @@ -278,7 +278,7 @@ public static function breakLine(string $styles = '', array $properties = []): C
*/
public static function getRenderer(): OutputInterface
{
return self::$renderer ??= new ConsoleOutput();
return self::$renderer ??= new ConsoleOutput;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/ValueObjects/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ final class Node
/**
* A value object with helper methods for working with DOM node.
*/
public function __construct(private \DOMNode $node)
{
}
public function __construct(private \DOMNode $node) {}

/**
* Gets the value of the node.
Expand Down
11 changes: 5 additions & 6 deletions src/ValueObjects/Styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ final public function __construct(
private array $textModifiers = [],
private array $styleModifiers = [],
private array $defaultStyles = []
) {
}
) {}

/**
* @return $this
Expand Down Expand Up @@ -854,7 +853,7 @@ private function applyWidth(string $content): string

preg_match_all("/\n+/", $content, $matches);

$width *= count($matches[0] ?? []) + 1;
$width *= count($matches[0] ?? []) + 1; // @phpstan-ignore-line
$width += mb_strlen($matches[0][0] ?? '', 'UTF-8');

if ($length <= $width) {
Expand Down Expand Up @@ -940,7 +939,7 @@ private function applyStyling(string $content): string
/**
* Get the length of the text provided without the styling tags.
*/
public function getLength(string $text = null): int
public function getLength(?string $text = null): int
{
return mb_strlen(preg_replace(
self::STYLING_REGEX,
Expand Down Expand Up @@ -998,7 +997,6 @@ private static function calcWidthFromFraction(string $fraction, array $styles, a
throw new InvalidStyle(sprintf('Style [%s] is invalid.', "w-$fraction"));
}

/** @@phpstan-ignore-next-line */
$width = (int) floor($width * $matches[1] / $matches[2]);
$width -= ($styles['ml'] ?? 0) + ($styles['mr'] ?? 0);

Expand Down Expand Up @@ -1030,7 +1028,7 @@ public static function getParentWidth(array $styles): int

$width = count($matches) !== 3
? (int) $parentWidth
: (int) floor($width * $matches[1] / $matches[2]); //@phpstan-ignore-line
: (int) floor($width * $matches[1] / $matches[2]);

if ($maxWidth > 0) {
$width = min($maxWidth, $width);
Expand All @@ -1052,6 +1050,7 @@ private static function trimText(string $text, int $width): string
preg_match_all(self::STYLING_REGEX, $text, $matches, PREG_OFFSET_CAPTURE);
$text = rtrim(mb_strimwidth(preg_replace(self::STYLING_REGEX, '', $text) ?? '', 0, $width, '', 'UTF-8'));

// @phpstan-ignore-next-line
foreach ($matches[0] ?? [] as [$part, $index]) {
$text = substr($text, 0, $index).$part.substr($text, $index, null);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
use Termwind\HtmlRenderer;
use Termwind\Repositories\Styles;

use function Termwind\{renderUsing};
use function Termwind\renderUsing;

uses()->beforeEach(fn () => renderUsing($this->output = new BufferedOutput()))
uses()->beforeEach(fn () => renderUsing($this->output = new BufferedOutput))
->afterEach(function () {
renderUsing(null);

Expand Down
2 changes: 0 additions & 2 deletions tests/a.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use function Termwind\{a};

it('renders the element', function () {
$html = parse('<a>link text</a>');

Expand Down
2 changes: 1 addition & 1 deletion tests/ask.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
$output->shouldReceive('write')->once()->with($clearLineCode);
$output->shouldReceive('write')->once()->with('<hl>ne</hl>');
$output->shouldReceive('write')->once()->with($restorePositionCode);
$output->shouldReceive('getFormatter')->once()->andReturn(new NullOutputFormatter());
$output->shouldReceive('getFormatter')->once()->andReturn(new NullOutputFormatter);

ask('<span class="bg-red ml-1">Question</span>', ['one', 'two', 'three']);
})->skip(! Terminal::hasSttyAvailable(), '`stty` is required to test autocomplete functionality');
2 changes: 0 additions & 2 deletions tests/br.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use function Termwind\{br};

it('renders the element', function () {
$html = parse('<br/>');

Expand Down
Loading

0 comments on commit 20122c2

Please sign in to comment.