Skip to content

Commit

Permalink
Add rector
Browse files Browse the repository at this point in the history
  • Loading branch information
cmen committed Oct 25, 2024
1 parent 3935cd9 commit 72d6f87
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 83 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
- name: Run PHPStan
run: composer analyse

- name: Run Rector
run: ./vendor/bin/rector process --dry-run --no-progress-bar

tests:
strategy:
matrix:
Expand Down
9 changes: 1 addition & 8 deletions GoogleCharts/Charts/Diff/DiffPieChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ class DiffPieChart extends PieChart implements DiffChart
*/
protected ChartOptionsInterface $options;

private PieChart $oldChart;

private PieChart $newChart;

public function __construct(PieChart $oldChart, PieChart $newChart)
public function __construct(private readonly PieChart $oldChart, private readonly PieChart $newChart)
{
parent::__construct();

$this->options = new DiffPieChartOptions();

$this->oldChart = $oldChart;
$this->newChart = $newChart;
}

public function getOptions(): DiffPieChartOptions
Expand Down
9 changes: 1 addition & 8 deletions GoogleCharts/Charts/Diff/DiffScatterChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,11 @@ class DiffScatterChart extends ScatterChart implements DiffChart
*/
protected ChartOptionsInterface $options;

private ScatterChart $oldChart;

private ScatterChart $newChart;

public function __construct(ScatterChart $oldChart, ScatterChart $newChart)
public function __construct(private readonly ScatterChart $oldChart, private readonly ScatterChart $newChart)
{
parent::__construct();

$this->options = new DiffScatterChartOptions();

$this->oldChart = $oldChart;
$this->newChart = $newChart;
}

public function getOptions(): DiffScatterChartOptions
Expand Down
5 changes: 1 addition & 4 deletions GoogleCharts/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ class Events
*/
protected array $listeners;

protected Chart $chart;

public function __construct(Chart $chart)
public function __construct(protected Chart $chart)
{
$this->listeners = [];
$this->chart = $chart;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions GoogleCharts/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@ class Listener
{
protected string $type;

protected string $functionName;

/**
* @throws GoogleChartsException
*/
public function __construct(string $type, string $functionName)
public function __construct(string $type, protected string $functionName)
{
if (!in_array($type, EventType::getAllEventTypes())) {
throw new GoogleChartsException("$type is not a valid type of event.");
}

$this->type = $type;
$this->functionName = $functionName;
}

public function getType(): string
Expand Down
24 changes: 10 additions & 14 deletions Output/AbstractChartOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@
*/
abstract class AbstractChartOutput implements ChartOutputInterface
{
/**
* Version of Google Charts used.
*/
protected string $version;

/**
* Locale to customize currencies, dates, and numbers.
*/
protected string $language;

public function __construct(string $version, string $language)
{
$this->version = $version;
$this->language = $language;
public function __construct(
/**
* Version of Google Charts used.
*/
protected string $version,
/**
* Locale to customize currencies, dates, and numbers.
*/
protected string $language,
) {
}

/**
Expand Down
4 changes: 1 addition & 3 deletions Output/AbstractOptionsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ abstract class AbstractOptionsOutput implements OptionsOutputInterface
{
public function removeRecursivelyNullValue(&$options): void
{
$options = array_filter((array) $options, function ($val) {
return !is_null($val);
});
$options = array_filter((array) $options, fn ($val) => !is_null($val));

foreach ($options as $key => $value) {
if (is_object($value) || is_array($value)) {
Expand Down
18 changes: 4 additions & 14 deletions Output/Javascript/ChartOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,14 @@
*/
class ChartOutput extends AbstractChartOutput
{
private OptionsOutputInterface $optionsOutput;

private DataOutputInterface $dataOutput;

private EventsOutputInterface $eventsOutput;

public function __construct(
string $version,
string $language,
OptionsOutputInterface $optionsOutput,
DataOutputInterface $dataOutput,
EventsOutputInterface $eventsOutput,
private readonly OptionsOutputInterface $optionsOutput,
private readonly DataOutputInterface $dataOutput,
private readonly EventsOutputInterface $eventsOutput,
) {
parent::__construct($version, $language);

$this->optionsOutput = $optionsOutput;
$this->dataOutput = $dataOutput;
$this->eventsOutput = $eventsOutput;
}

public function startChart(Chart $chart): string
Expand Down Expand Up @@ -151,7 +141,7 @@ public function fullCharts($charts, $elementsID = null): string

public function loadLibraries(array $packages): string
{
array_walk($packages, function (&$item) {
array_walk($packages, function (&$item): void {
$item = "'".$item."'";
});

Expand Down
13 changes: 3 additions & 10 deletions Output/Javascript/DataOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
*/
class DataOutput implements DataOutputInterface
{
private DateOutputInterface $dateOutput;

public function __construct(DateOutputInterface $dateOutput)
public function __construct(private readonly DateOutputInterface $dateOutput)
{
$this->dateOutput = $dateOutput;
}

public function draw(Data $data, string $dataName): string
Expand All @@ -28,14 +25,10 @@ public function draw(Data $data, string $dataName): string
}

$js = "var $dataName = new google.visualization.arrayToDataTable([";

end($arrayToDataTable);
$lastKeyRow = key($arrayToDataTable);
$lastKeyRow = array_key_last($arrayToDataTable);
foreach ($data->getArrayToDataTable() as $keyRow => $row) {
$js .= '[';

end($row);
$lastKeyValue = key($row);
$lastKeyValue = array_key_last($row);
foreach ($row as $key => $value) {
if ($value instanceof \DateTimeInterface) {
$js .= $this->dateOutput->draw($value);
Expand Down
9 changes: 2 additions & 7 deletions Output/Javascript/OptionsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@
*/
class OptionsOutput extends AbstractOptionsOutput
{
private DateOutputInterface $dateOutput;

public function __construct(DateOutputInterface $dateOutput)
public function __construct(private readonly DateOutputInterface $dateOutput)
{
$this->dateOutput = $dateOutput;
}

public function draw(ChartOptionsInterface $options, string $optionsName): string
Expand All @@ -28,9 +25,7 @@ public function draw(ChartOptionsInterface $options, string $optionsName): strin
$options = $this->renameRecursivelyKeys($options);

$js = "var $optionsName = {";

end($options);
$lastKey = key($options);
$lastKey = array_key_last($options);
foreach ($options as $optionKey => $optionValue) {
$js .= '"'.$optionKey.'":';

Expand Down
3 changes: 2 additions & 1 deletion Resources/doc/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

Before opening a pull request, be sure to follow the steps below.

## Step 1 : check and correct the code using PHPStan
## Step 1 : check and correct the code using PHPStan and Rector
```bash
composer analyse
composer rector
```

## Step 2 : run tests using PHPUnit and check all tests are OK
Expand Down
15 changes: 6 additions & 9 deletions Twig/GoogleChartsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
*/
class GoogleChartsExtension extends AbstractExtension
{
private ChartOutputInterface $chartOutput;

public function __construct(ChartOutputInterface $chartOutput)
public function __construct(private readonly ChartOutputInterface $chartOutput)
{
$this->chartOutput = $chartOutput;
}

public function getFunctions(): array
{
return [
new TwigFunction('gc_draw', [$this, 'gcDraw'], ['is_safe' => ['html']]),
new TwigFunction('gc_start', [$this, 'gcStart'], ['is_safe' => ['html']]),
new TwigFunction('gc_end', [$this, 'gcEnd'], ['is_safe' => ['html']]),
new TwigFunction('gc_event', [$this, 'gcEvent'], ['is_safe' => ['html']]),
new TwigFunction('gc_language', [$this, 'gcLanguage']),
new TwigFunction('gc_draw', $this->gcDraw(...), ['is_safe' => ['html']]),
new TwigFunction('gc_start', $this->gcStart(...), ['is_safe' => ['html']]),
new TwigFunction('gc_end', $this->gcEnd(...), ['is_safe' => ['html']]),
new TwigFunction('gc_event', $this->gcEvent(...), ['is_safe' => ['html']]),
new TwigFunction('gc_language', $this->gcLanguage(...)),
];
}

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.64",
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^10.5"
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.2"
},
"autoload": {
"psr-4": { "CMEN\\GoogleChartsBundle\\": "" }
Expand All @@ -33,6 +34,7 @@
},
"scripts": {
"analyze": "./vendor/bin/phpstan analyse",
"rector": "./vendor/bin/rector process",
"lint": "./vendor/bin/php-cs-fixer fix --diff -vv",
"tests": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --testdox"
}
Expand Down
16 changes: 16 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withPaths([
__DIR__.'/DependencyInjection',
__DIR__.'/Exception',
__DIR__.'/GoogleCharts',
__DIR__.'/Output',
__DIR__.'/Tests',
__DIR__.'/Twig',
])
->withPhpSets(php81: true);

0 comments on commit 72d6f87

Please sign in to comment.