Skip to content

Commit

Permalink
Add rector [batch] (#43)
Browse files Browse the repository at this point in the history
* Raise minimum PHP version to ^8.0
* Cleanup code
Co-authored-by: Rustam <rmamdaminov@gmail.com>
  • Loading branch information
xepozz committed Dec 8, 2022
1 parent e942d11 commit 269b3be
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
21 changes: 21 additions & 0 deletions .github/workflows/rector.yml
@@ -0,0 +1,21 @@
on:
pull_request:
paths-ignore:
- 'docs/**'
- 'README.md'
- 'CHANGELOG.md'
- '.gitignore'
- '.gitattributes'
- 'infection.json.dist'
- 'psalm.xml'

name: rector

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector.yml@master
with:
os: >-
['ubuntu-latest']
php: >-
['8.0']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['7.4', '8.0', '8.1']
['8.0', '8.1']
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 2.0.2 under development

- Enh #41: Add `yiisoft/view` of version `^5.0` and `^6.0` support (@vjik)
- Chg #43: Raise minimum PHP version to `^8.0` (@xepozz, @rustamwin)

## 2.0.1 October 25, 2021

Expand Down
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,10 @@
The package is an extension of the [Yii View Rendering Library](https://github.com/yiisoft/view/). This extension
provides a `ViewRender` that would allow you to use [Twig](https://twig.symfony.com/) view template engine.

## Requirements

- PHP 8.0 or higher.

## Installation

The package could be installed with composer:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -17,13 +17,14 @@
"source": "https://github.com/yiisoft/view-twig"
},
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"psr/container": "^1.0",
"twig/twig": "^3.3",
"yiisoft/view": "^4.0|^5.0|^6.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"rector/rector": "^0.14.3",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.18",
Expand Down
22 changes: 22 additions & 0 deletions rector.php
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);
};
8 changes: 2 additions & 6 deletions src/Extensions/YiiTwigExtension.php
Expand Up @@ -13,11 +13,8 @@
*/
final class YiiTwigExtension extends AbstractExtension
{
private ContainerInterface $container;

public function __construct(ContainerInterface $container)
public function __construct(private ContainerInterface $container)
{
$this->container = $container;
}

/**
Expand All @@ -28,8 +25,7 @@ public function getFunctions(): array
return [
new TwigFunction(
'get',
/** @return mixed */
fn (string $id) => $this->container->get($id),
fn (string $id): mixed => $this->container->get($id),
),
];
}
Expand Down
10 changes: 3 additions & 7 deletions src/ViewRenderer.php
Expand Up @@ -22,11 +22,8 @@
*/
final class ViewRenderer implements TemplateRendererInterface
{
private Environment $environment;

public function __construct(Environment $environment)
public function __construct(private Environment $environment)
{
$this->environment = $environment;
}

public function render(ViewInterface $view, string $template, array $parameters): string
Expand All @@ -42,12 +39,11 @@ public function render(ViewInterface $view, string $template, array $parameters)

$obInitialLevel = ob_get_level();
ob_start();
/** @psalm-suppress InvalidArgument */
PHP_VERSION_ID >= 80000 ? ob_implicit_flush(false) : ob_implicit_flush(0);
ob_implicit_flush(false);

try {
/** @psalm-suppress PossiblyInvalidFunctionCall */
$renderer->bindTo($view)($template, $parameters);
$renderer->bindTo($view)();
return ob_get_clean();
} catch (Throwable $e) {
while (ob_get_level() > $obInitialLevel) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ViewTest.php
Expand Up @@ -78,7 +78,7 @@ public function testExceptionDuringRendering(): void

try {
$renderer->render($view, __DIR__ . '/public/views/error.twig', []);
} catch (RuntimeError $e) {
} catch (RuntimeError) {
}

$this->assertSame(ob_get_level(), $obInitialLevel);
Expand Down

0 comments on commit 269b3be

Please sign in to comment.