Skip to content

Commit d2513da

Browse files
authored
Merge pull request #1 from jijihohococo/feature/phpcs
Adding phpcs
2 parents 9d23159 + b178ba1 commit d2513da

File tree

3 files changed

+162
-60
lines changed

3 files changed

+162
-60
lines changed

.github/workflows/php-quality.yaml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: PHP Auto-Fix & Quality Check
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
types: [opened, synchronize, reopened]
9+
10+
jobs:
11+
# 🔧 Auto-fix coding standards (only once, PHP 8.1)
12+
phpcbf-fix:
13+
runs-on: ubuntu-latest
14+
steps:
15+
# 1️⃣ Checkout code
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
with:
19+
ref: ${{ github.head_ref || github.ref_name }}
20+
persist-credentials: false # allows bot to push fixes
21+
22+
# 2️⃣ Setup PHP (single version for auto-fix)
23+
- name: Setup PHP 8.3
24+
uses: shivammathur/setup-php@v2
25+
with:
26+
php-version: 8.3
27+
coverage: none
28+
29+
# 3️⃣ Install PHPCS
30+
- name: Install PHPCS
31+
run: |
32+
composer global require squizlabs/php_codesniffer
33+
echo "$HOME/.config/composer/vendor/bin" >> $GITHUB_PATH
34+
35+
# 4️⃣ Run PHPCBF (auto-fix issues)
36+
- name: Run PHPCBF
37+
run: phpcbf --standard=PSR12 src/ || true
38+
39+
# 5️⃣ Commit & push fixes back to branch
40+
- name: Commit and push PHPCBF fixes
41+
if: github.ref != 'refs/heads/main' # optional: skip main
42+
env:
43+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44+
run: |
45+
git config user.name "github-actions[bot]"
46+
git config user.email "github-actions[bot]@users.noreply.github.com"
47+
git add .
48+
# commit only if there are changes
49+
if ! git diff-index --quiet HEAD; then
50+
git commit -m "PHPCBF: auto-fix coding standards"
51+
# determine branch for PRs or direct pushes
52+
BRANCH="${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}"
53+
# pull remote changes to avoid rejection
54+
git fetch origin $BRANCH
55+
git rebase origin/$BRANCH
56+
# push fixes
57+
git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:$BRANCH
58+
else
59+
echo "No changes to commit, skipping push."
60+
fi
61+
62+
# 🧪 Run PHPCS + PHPStan on multiple PHP versions
63+
php-quality:
64+
runs-on: ubuntu-latest
65+
needs: phpcbf-fix
66+
strategy:
67+
fail-fast: false
68+
matrix:
69+
php-version: [7.3, 7.4, 8.0, 8.1, 8.2, 8.3]
70+
71+
steps:
72+
# 1️⃣ Checkout code
73+
- name: Checkout code
74+
uses: actions/checkout@v4
75+
76+
# 2️⃣ Setup PHP for matrix
77+
- name: Setup PHP ${{ matrix.php-version }}
78+
uses: shivammathur/setup-php@v2
79+
with:
80+
php-version: ${{ matrix.php-version }}
81+
coverage: none
82+
83+
# 3️⃣ Install PHPStan + PHPCS
84+
- name: Install PHPStan and PHPCS
85+
run: |
86+
composer global require phpstan/phpstan squizlabs/php_codesniffer
87+
echo "$HOME/.config/composer/vendor/bin" >> $GITHUB_PATH
88+
89+
# 4️⃣ Run PHPStan
90+
- name: Run PHPStan
91+
run: phpstan analyse src --memory-limit=1G
92+
93+
# 5️⃣ Run PHPCS
94+
- name: Run PHPCS (lint only)
95+
run: phpcs --standard=PSR12 --warning-severity=0 src/

src/ENV.php

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,41 @@
22

33
namespace JiJiHoHoCoCo\PHPENV;
44

5-
class ENV{
6-
7-
private static $env , $comma ;
8-
private static $dataset=[];
9-
10-
public static function set(string $filePath){
11-
if(is_readable($filePath)){
12-
self::$comma=rand();
13-
self::$env=implode(self::$comma,file($filePath) );
14-
}else{
15-
throw new Exception($filePath . ' is not exist', 1);
16-
}
17-
}
18-
19-
public static function get(){
20-
return self::$env;
21-
}
22-
23-
public static function getComma(){
24-
return self::$comma;
25-
}
26-
27-
public static function setDataset(string $key,$data){
28-
self::$dataset[$key]=$data;
29-
}
30-
31-
public static function getDataset(){
32-
return self::$dataset;
33-
}
34-
}
5+
use Exception;
6+
7+
class ENV
8+
{
9+
private static $env;
10+
private static $comma;
11+
private static $dataset = [];
12+
13+
public static function set(string $filePath)
14+
{
15+
if (is_readable($filePath)) {
16+
self::$comma = rand();
17+
self::$env = implode(self::$comma, file($filePath));
18+
} else {
19+
throw new Exception($filePath . ' is not exist', 1);
20+
}
21+
}
22+
23+
public static function get()
24+
{
25+
return self::$env;
26+
}
27+
28+
public static function getComma()
29+
{
30+
return self::$comma;
31+
}
32+
33+
public static function setDataset(string $key, $data)
34+
{
35+
self::$dataset[$key] = $data;
36+
}
37+
38+
public static function getDataset()
39+
{
40+
return self::$dataset;
41+
}
42+
}

src/helpers.php

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@
22

33
use JiJiHoHoCoCo\PHPENV\ENV;
44

5-
if(!function_exists('getStringBetween')){
6-
function getStringBetween (string $str,string $from,string $to) : string {
5+
if (!function_exists('getStringBetween')) {
6+
function getStringBetween(string $str, string $from, string $to): string
7+
{
78

8-
$string = substr($str, strpos($str, $from) + strlen($from));
9+
$string = substr($str, strpos($str, $from) + strlen($from));
910

10-
if (strstr ($string,$to,TRUE) != FALSE) {
11+
if (strstr($string, $to, true) != false) {
12+
$string = strstr($string, $to, true);
13+
}
1114

12-
$string = strstr ($string,$to,TRUE);
13-
14-
}
15-
16-
return $string;
17-
18-
}
15+
return $string;
16+
}
1917
}
2018

21-
if(!function_exists('gete')){
22-
function gete(string $data){
23-
$previousData=$data;
24-
$dataset=ENV::getDataset();
25-
if(isset($dataset[$data])){
26-
return preg_replace('/\s+/', '',$dataset[$data]);
27-
}else{
28-
$data=$data.'=';
29-
$env=ENV::get();
30-
$comma=ENV::getComma();
31-
if($env==NULL){
32-
throw new \Exception("Please set the file path firstly", 1);
33-
}
34-
$getData=strpos($env,$data)!==FALSE ? getStringBetween($env,$data,$comma) : NULL;
35-
ENV::setDataset($previousData,$getData);
36-
return preg_replace('/\s+/', '',$getData);
37-
}
38-
}
39-
}
19+
if (!function_exists('gete')) {
20+
function gete(string $data)
21+
{
22+
$previousData = $data;
23+
$dataset = ENV::getDataset();
24+
if (isset($dataset[$data])) {
25+
return preg_replace('/\s+/', '', $dataset[$data]);
26+
} else {
27+
$data = $data . '=';
28+
$env = ENV::get();
29+
$comma = ENV::getComma();
30+
if ($env == null) {
31+
throw new \Exception("Please set the file path firstly", 1);
32+
}
33+
$getData = strpos($env, $data) !== false ? getStringBetween($env, $data, $comma) : null;
34+
ENV::setDataset($previousData, $getData);
35+
return preg_replace('/\s+/', '', $getData);
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)