Skip to content

Commit

Permalink
Merge pull request #33 from giggsey/cs
Browse files Browse the repository at this point in the history
🎨 Convert to PHP CS Fixer
  • Loading branch information
giggsey authored Nov 4, 2024
2 parents 40e5eb0 + 1bc6e91 commit a9893d8
Show file tree
Hide file tree
Showing 444 changed files with 56,091 additions and 57,336 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/build.xml export-ignore
/.styleci.yml export-ignore
/.php-cs-fixer.dist.php export-ignore
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ jobs:
strategy:
matrix:
php-version:
- "7.2"
- "7.3"
- "7.4"
- "8.0"
- "8.1"
- "8.2"
- "8.3"
- "8.4"
include:
- php-version: 8.2
- php-version: 8.3
coverage: coverage
steps:
- name: Checkout
Expand Down
42 changes: 42 additions & 0 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: PHP CS Fixer
on:
push:
branches:
- 'main'
pull_request:
paths:
- '**.php'

concurrency:
# The concurrency group contains the workflow name and the branch name.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
php-cs-fixer:
name: "PHP CS Fixer"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: 8.3

- uses: "ramsey/composer-install@v3"
with:
dependency-versions: highest

- name: Run PHP CS Fixer
run: "composer run-script cs"

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: ":art: Fix code styling"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ cldr-json/
data-git/
tests/testOutput/
/.phpunit.result.cache
/.php-cs-fixer.cache
24 changes: 24 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__);

$config = new PhpCsFixer\Config();
return $config->setRules(
[
'@PER-CS2.0' => true,
'@PHP74Migration' => true,
'single_quote' => true,
'no_unused_imports' => true,
'no_superfluous_phpdoc_tags' => [
'allow_hidden_params' => true,
'allow_mixed' => true,
'remove_inheritdoc' => true,
],
'phpdoc_trim' => true,
]
)
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
->setFinder($finder);
5 changes: 0 additions & 5 deletions .styleci.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Locale [![Build Status](https://img.shields.io/travis/giggsey/Locale.svg?style=flat-square)](https://travis-ci.org/giggsey/Locale) [![Coverage Status](https://img.shields.io/coveralls/giggsey/Locale.svg?style=flat-square)](https://coveralls.io/r/giggsey/Locale) [![StyleCI](https://styleci.io/repos/24566760/shield)](https://styleci.io/repos/24566760)
# Locale [![Build Status](https://github.com/giggsey/locale/workflows/Continuous%20Integration/badge.svg)](https://github.com/giggsey/locale/actions?query=workflow%3A%22Continuous+Integration%22) [![Coverage Status](https://img.shields.io/coveralls/giggsey/Locale.svg?style=flat-square)](https://coveralls.io/r/giggsey/Locale)

[![Total Downloads](https://poser.pugx.org/giggsey/locale/downloads?format=flat-square)](https://packagist.org/packages/giggsey/locale) [![Latest Stable Version](https://poser.pugx.org/giggsey/locale/v/stable?format=flat-square)](https://packagist.org/packages/giggsey/locale) [![License](https://poser.pugx.org/giggsey/locale/license?format=flat-square)](https://packagist.org/packages/giggsey/locale)

Expand Down
7 changes: 6 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</target>

<target name="compile" description="Build data"
depends="cleanup-data,build-data">
depends="cleanup-data,build-data,cs-fixer">
</target>

<target name="cleanup-data" description="Cleanup built data">
Expand Down Expand Up @@ -65,4 +65,9 @@
</exec>
</target>

<target name="cs-fixer">
<exec executable="vendor/bin/php-cs-fixer" passthru="true">
<arg value="fix"/>
</exec>
</target>
</project>
60 changes: 20 additions & 40 deletions build/Build/DataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
use RuntimeException;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\VarExporter\VarExporter;

class DataBuilder
{
protected const GENERATION_HEADER = <<<EOT
/**
* Locale @generated from CLDR version {{ version }}
* See README.md for more information.
*
* @internal
*
* Do not modify or use this file directly!
*/
/**
* Locale data file
* This file has been @generated from Locale data
* Do not modify or use this file directly!
* @internal
*/
EOT;
EOT;

/**
* Ignore these locales
Expand Down Expand Up @@ -55,7 +53,6 @@ class DataBuilder
* @param string $version Version of the CLDR data
* @param string $inputDir Input directory to load CLDR data from
* @param string $outputDir Output directory to write data
* @param OutputInterface $output
*/
public function generate(string $version, string $inputDir, string $outputDir, OutputInterface $output): void
{
Expand Down Expand Up @@ -102,7 +99,7 @@ public function generate(string $version, string $inputDir, string $outputDir, O
continue;
}

$this->writeTerritoryFile($outputDir, $version, $locale, $countryData);
$this->writeTerritoryFile($outputDir, $locale, $countryData);

$writtenCountries[strtolower($locale)] = '';

Expand All @@ -111,7 +108,7 @@ public function generate(string $version, string $inputDir, string $outputDir, O

ksort($writtenCountries);

$this->writeMappingFile($outputDir, $version, $writtenCountries);
$this->writeMappingFile($outputDir, $writtenCountries);

$this->writeVersionFile($outputDir, $version);

Expand All @@ -121,19 +118,17 @@ public function generate(string $version, string $inputDir, string $outputDir, O
/**
* Check and create directories
*
* @param string $inputDir
* @param string $outputDir
* @codeCoverageIgnore
*/
private function checkDirectories(string $inputDir, string $outputDir): void
{
if (!is_dir($inputDir)) {
throw new InvalidArgumentException(sprintf("Unable to find input directory: %s", $inputDir));
throw new InvalidArgumentException(sprintf('Unable to find input directory: %s', $inputDir));
}

// Try to create output directory
if (!is_dir($outputDir) && !mkdir($outputDir) && !is_dir($outputDir)) {
throw new RuntimeException(sprintf("Unable to create output directory: %s", $outputDir));
throw new RuntimeException(sprintf('Unable to create output directory: %s', $outputDir));
}
}

Expand Down Expand Up @@ -163,10 +158,6 @@ private function loadLocales(string $inputDir): array

/**
*
*
* @param string $inputDir
* @param array $localeList
* @return array
*/
protected function loadTerritories(string $inputDir, array $localeList): array
{
Expand Down Expand Up @@ -213,29 +204,23 @@ protected function loadTerritories(string $inputDir, array $localeList): array
return $countries;
}

/**
* @param string $outputDir
* @param string $version CLDR Version
* @param string $locale
* @param array $data
*/
private function writeTerritoryFile(string $outputDir, string $version, string $locale, array $data): void
private function writeTerritoryFile(string $outputDir, string $locale, array $data): void
{
$phpSource = '<?php'
. PHP_EOL
. $this->generateFileHeader($version)
. 'return ' . var_export($data, true) . ';'
. static::GENERATION_HEADER
. 'return ' . VarExporter::export($data) . ';'
. PHP_EOL;

file_put_contents($outputDir . strtolower($locale) . '.php', $phpSource);
}

private function writeMappingFile(string $outputDir, string $version, array $countryList): void
private function writeMappingFile(string $outputDir, array $countryList): void
{
$phpSource = '<?php'
. PHP_EOL
. $this->generateFileHeader($version)
. 'return ' . var_export($countryList, true) . ';'
. static::GENERATION_HEADER
. 'return ' . VarExporter::export($countryList) . ';'
. PHP_EOL;

file_put_contents($outputDir . '_list.php', $phpSource);
Expand All @@ -245,15 +230,10 @@ private function writeVersionFile(string $outputDir, string $version): void
{
$phpSource = '<?php'
. PHP_EOL
. $this->generateFileHeader($version)
. 'return ' . var_export($version, true) . ';'
. static::GENERATION_HEADER
. 'return ' . VarExporter::export($version) . ';'
. PHP_EOL;

file_put_contents($outputDir . '_version.php', $phpSource);
}

private function generateFileHeader($version): string
{
return str_replace('{{ version }}', $version, static::GENERATION_HEADER);
}
}
14 changes: 8 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": ">=7.2"
"php": "^7.4|^8.0"
},
"require-dev": {
"ext-json": "*",
Expand All @@ -24,7 +24,9 @@
"symfony/console": "^5.0|^6.0",
"symfony/filesystem": "^5.0|^6.0",
"symfony/process": "^5.0|^6.0",
"symfony/finder": "^5.0|^6.0"
"symfony/finder": "^5.0|^6.0",
"symfony/var-exporter": "^5.2|^6.0",
"friendsofphp/php-cs-fixer": "^3.64"
},
"autoload": {
"psr-4": {
Expand All @@ -43,14 +45,14 @@
"tests/",
".gitattributes",
".gitignore",
".styleci.yml",
".travis.yml",
".php-cs-fixer.dist.php",
"build.xml",
"phpunit.xml.dist"
]
},
"scripts": {
"test": "phpunit",
"metadata": "phing compile"
"cs": "vendor/bin/php-cs-fixer fix",
"test": "vendor/bin/phpunit",
"metadata": "vendor/bin/phing compile"
}
}
Loading

0 comments on commit a9893d8

Please sign in to comment.