Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rector, csfixer, phpstan #10

Merged
merged 6 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 74 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,82 @@ env:
DEFAULT_COMPOSER_FLAGS: "--prefer-dist --no-interaction"
CC_TEST_REPORTER_ID: a3e21784ef59ddcf7c4e0c56ebb9a0a31131d96d6cb78972848ac4b628a4191f
jobs:
## PHPSTAN
phpstan:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@2.21.0
with:
php-version: '8.0'
extensions: mbstring, intl
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update: true

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: PHPStan tests
run: composer phpstan

## Rector
rector:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install PHP
uses: shivammathur/setup-php@2.21.0
with:
php-version: '8.0'
extensions: mbstring, intl
coverage: none
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update: true

- name: Install dependencies
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader

- name: Rector tests
run: composer rector

- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply rector changes

# PHPCSFIXER
php-cs-fixer:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install
run: composer install

- name: Run php-cs-fixer
run: composer phpcsfixer

- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Apply php-cs-fixer changes

# PHPUNIT
phpunit:
name: PHP ${{ matrix.php }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
php: ['7.4', '8.0', '8.1']

steps:
## checkout the repoistory
Expand All @@ -25,7 +93,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, dom, imagick, intl, mbstring, mcrypt, mysql, pdo, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, sqlite, zip
extensions: curl, dom, intl, mbstring, sqlite, zip
ini-values: date.timezone='UTC'
coverage: xdebug

Expand All @@ -36,18 +104,18 @@ jobs:
## run unit tests
- name: PHP Unit tests for PHP
run: vendor/bin/phpunit --verbose --configuration actions.phpunit.xml
if: matrix.php == '8.1' || matrix.php == '8.0' || matrix.php == '7.4' || matrix.php == '7.3' || matrix.php == '7.2' || matrix.php == '7.0'
if: matrix.php == '8.1' || matrix.php == '8.0'

## unit test with coverage
- name: PHP Unit tests for PHP 7.1
- name: PHP Unit tests for PHP 7.4 Coverage
run: vendor/bin/phpunit --verbose --coverage-clover=clover.xml --configuration actions.phpunit.xml
if: matrix.php == '7.1'
if: matrix.php == '7.4'

## coverage
- name: Code coverage
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
./cc-test-reporter after-build -t clover
if: matrix.php == '7.1'
if: matrix.php == '7.4'
continue-on-error: true # if is fork
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
luya
composer.lock
.phpunit*
.php*
.php-cs-fixer.cache
.php_cs.cache
.tmp
16 changes: 16 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
])
->setFinder($finder)
;
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).

## 1.2.3 ()
## 1.3.0 ()

+ [#10](https://github.com/luyadev/yii-helpers/pull/10) Add rector, phpstan and phpcsfixer, therefore raised php test version to 7.4
+ [#9](https://github.com/luyadev/yii-helpers/pull/9) Fixed issue when a word exists inside a highlight word in function `StringHelper::highlightWord()`

## 1.2.2 (20. July 2022)
Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
parameters:
level: 1
level: 5
paths:
- src
bootstrapFiles:
- vendor/yiisoft/yii2/Yii.php
tmpDir: .tmp
tmpDir: .tmp
excludePaths:
- src/helpers/XLSXWriter.php
2 changes: 1 addition & 1 deletion src/helpers/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function toObject(array $array)
* ```
*
* @param array $data The input data to cover given sensitive key values. `['username' => 'foo', 'password' => 'bar']`.
* @param array $key The keys which can contain sensitive data inside the $data array. `['password', 'pwd', 'pass']` if no keys provided the {{luya\yii\helpers\ArrayHelper::$sensitiveDefaultKeys}} is used.
* @param array $keys The keys which can contain sensitive data inside the $data array. `['password', 'pwd', 'pass']` if no keys provided the {{luya\yii\helpers\ArrayHelper::$sensitiveDefaultKeys}} is used.
*/
public static function coverSensitiveValues(array $data, array $keys = [])
{
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/ExportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ExportHelper
*
* @param array|QueryInterface $input The data to export into a csv
* @param array $keys Defines which keys should be packed into the generated CSV. The defined keys does not change the sort behavior of the generated csv.
* @param string $header Whether the column name should be set as header inside the csv or not.
* @param boolean $header Whether the column name should be set as header inside the csv or not.
* @param array $options Options
* + `sort`: boolean, whether they row should be sorted by its keys, default is true.
* @return string The generated CSV as string.
Expand Down Expand Up @@ -74,8 +74,7 @@ protected static function transformInput($input)
/**
* Generate content by rows.
*
* @param array $contentRows
* @param string $delimiter
* @param array|string $contentRows
* @param array $keys
* @param bool $generateHeader
* @param array $options Options
Expand Down
11 changes: 5 additions & 6 deletions src/helpers/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public static function ensureExtension($file, $extension)
return $file;
}



/**
* Provide class informations from a file path or file content.
*
Expand Down Expand Up @@ -115,7 +113,7 @@ private static function classNameByTokens($phpCode)
* Because PHP's integer type is signed many crc32 checksums will result in negative integers on 32bit platforms. On 64bit installations all crc32() results will be positive integers though.
* So you need to use the "%u" formatter of sprintf() or printf() to get the string representation of the unsigned crc32() checksum in decimal format.
*
* @var string $fileName The file name which should be hashed
* @param string $fileName The file name which should be hashed
* @return string
*/
public static function hashName($fileName)
Expand All @@ -131,13 +129,14 @@ public static function hashName($fileName)
*/
public static function getFileInfo($sourceFile)
{
// pathinfo always returns an array event the path does not exists
$path = pathinfo($sourceFile);

return (object) [
'extension' => (isset($path['extension']) && !empty($path['extension'])) ? mb_strtolower($path['extension'], 'UTF-8') : false,
'name' => (isset($path['filename']) && !empty($path['filename'])) ? $path['filename'] : false,
'extension' => !empty($path['extension']) ? mb_strtolower($path['extension'], 'UTF-8') : false,
'name' => !empty($path['filename']) ? $path['filename'] : false,
'source' => $sourceFile,
'sourceFilename' => (isset($path['dirname']) && isset($path['filename'])) ? $path['dirname'] . DIRECTORY_SEPARATOR . $path['filename'] : false,
'sourceFilename' => !empty($path['filename']) ? $path['dirname'] . DIRECTORY_SEPARATOR . $path['filename'] : false,
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/ImportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ImportHelper
* 'fields' => ['firstname', 'lastname'] // will only parse those fields based on table header (row 0)
* 'fields' => [0,1,3] // will only parse fields by those positions if no table header is present. Positions starts at 0
* ```
* @return Returns an array with the csv data.
* @return array an array with the csv data.
*/
public static function csv($filename, array $options = [])
{
Expand Down
3 changes: 1 addition & 2 deletions src/helpers/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ public static function isJson($value)

$firstChar = substr($value, 0, 1);


if ($firstChar !== '{' && $firstChar !== '[') {
return false;
}

$json_check = json_decode($value);
json_decode($value);

return json_last_error() === JSON_ERROR_NONE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static function startsWithWildcard($string, $with, $caseSensitive = true)
* ```
*
* @param $value The value on which the filter conditions should be applied on.
* @param array|string $filters An array of filter conditions, if a string is given he will be exploded by commas.
* @param array|string $conditions An array of filter conditions, if a string is given he will be exploded by commas.
* @param boolean $caseSensitive Whether to match value even when lower/upper case is not correct/same.
* @return bool Returns true if one of the given filter conditions matches.
*/
Expand Down
7 changes: 5 additions & 2 deletions src/helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace luya\yii\helpers;

use luya\web\UrlManager;
use Yii;
use yii\helpers\BaseUrl;

Expand Down Expand Up @@ -47,11 +48,13 @@ public static function trailing($url, $slash = '/')
*/
public static function toInternal(array $routeParams, $scheme = false)
{
/** @var UrlManager $urlManager */
$urlManager = Yii::$app->getUrlManager();
if ($scheme) {
return Yii::$app->getUrlManager()->internalCreateAbsoluteUrl($routeParams);
return $urlManager->internalCreateAbsoluteUrl($routeParams);
}

return Yii::$app->getUrlManager()->internalCreateUrl($routeParams);
return $urlManager->internalCreateUrl($routeParams);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/XLSXWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ protected function styleFontIndexes()
$semi_colon_pos = strpos($cell_style_string, ";");
$number_format_idx = substr($cell_style_string, 0, $semi_colon_pos);
$style_json_string = substr($cell_style_string, $semi_colon_pos + 1);
$style = @json_decode($style_json_string, $as_assoc = true, 512, JSON_THROW_ON_ERROR);
try {
$style = @json_decode($style_json_string, $as_assoc = true, 512, JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
$style = [];
}

$style_indexes[$i] = ['num_fmt_idx' => $number_format_idx];//initialize entry
if (isset($style['border']) && is_string($style['border'])) {//border is a comma delimited str
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public function testIsJson()
$this->assertFalse(Json::isJson('12312312'));
$this->assertFalse(Json::isJson(12312312));
$this->assertFalse(Json::isJson('luya{"123":123}'));
$this->assertFalse(Json::isJson('{"123":\'123}'));
$this->assertFalse(Json::isJson('{"1233}'));
$this->assertFalse(Json::isJson('{"1232"3}'));
// is a json
$this->assertTrue(Json::isJson('{"123":"456"}'));
$this->assertTrue(Json::isJson('{"123":456}'));
Expand Down