Skip to content

Commit

Permalink
Merge pull request #3680 from samsonasik/apply-rector-1
Browse files Browse the repository at this point in the history
apply rector: set variable name to camel case
  • Loading branch information
MGatner authored Sep 29, 2020
2 parents 4330d05 + cf6e11d commit 5425519
Show file tree
Hide file tree
Showing 61 changed files with 958 additions and 739 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/test-rector.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# When a PR is opened or a push is made, perform
# a static analysis check on the code using Rector.
name: Rector

on:
pull_request:
branches:
- 'develop'
- '4.*'
paths:
- 'app/**'
- 'system/**'
push:
branches:
- 'develop'
- '4.*'
paths:
- 'app/**'
- 'system/**'

jobs:
build:
name: Analyze code (Rector)
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: intl

- name: Use latest Composer
run: composer self-update

- name: Validate composer.json
run: composer validate --strict

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Create composer cache directory
run: mkdir -p ${{ steps.composer-cache.outputs.dir }}

- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --ansi --no-progress --no-suggest --no-interaction

- name: Run static analysis
run: vendor/bin/rector process --dry-run
10 changes: 5 additions & 5 deletions app/Config/Mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,19 +497,19 @@ public static function guessTypeFromExtension(string $extension)
* Attempts to determine the best file extension for a given mime type.
*
* @param string $type
* @param string|null $proposed_extension - default extension (in case there is more than one with the same mime type)
* @param string|null $proposedExtension - default extension (in case there is more than one with the same mime type)
*
* @return string|null The extension determined, or null if unable to match.
*/
public static function guessExtensionFromType(string $type, string $proposed_extension = null)
public static function guessExtensionFromType(string $type, string $proposedExtension = null)
{
$type = trim(strtolower($type), '. ');

$proposed_extension = trim(strtolower($proposed_extension));
$proposedExtension = trim(strtolower($proposedExtension));

if ($proposed_extension !== '' && array_key_exists($proposed_extension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposed_extension]) ? [static::$mimes[$proposed_extension]] : static::$mimes[$proposed_extension]))
if ($proposedExtension !== '' && array_key_exists($proposedExtension, static::$mimes) && in_array($type, is_string(static::$mimes[$proposedExtension]) ? [static::$mimes[$proposedExtension]] : static::$mimes[$proposedExtension]))
{
return $proposed_extension;
return $proposedExtension;
}

foreach (static::$mimes as $ext => $types)
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@
"codeigniter4/codeigniter4-standard": "^1.0",
"fzaninotto/faker": "^1.9@dev",
"mikey179/vfsstream": "1.6.*",
"nette/utils": "^3.1",
"phpstan/phpstan": "^0.12",
"phpunit/phpunit": "^8.5",
"predis/predis": "^1.1",
"rector/rector-prefixed": "0.8.11",
"squizlabs/php_codesniffer": "^3.3"
},
"autoload": {
"psr-4": {
"CodeIgniter\\": "system/"
}
},
"autoload-dev": {
"psr-4": {
"Utils\\": "utils"
}
},
"scripts": {
"post-update-cmd": [
"@composer dump-autoload",
Expand Down
29 changes: 29 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Utils\Rector\UnderscoreToCamelCaseVariableNameRector;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

// paths to refactor; solid alternative to CLI arguments
$parameters->set(Option::PATHS, [__DIR__ . '/app', __DIR__ . '/system']);

// is there a file you need to skip?
$parameters->set(Option::EXCLUDE_PATHS, [
__DIR__ . '/app/Views',
__DIR__ . '/system/Autoloader/Autoloader.php',
__DIR__ . '/system/Debug/Toolbar/Views/toolbar.tpl.php',
__DIR__ . '/system/ThirdParty',
]);

// Rector relies on autoload setup of your project; Composer autoload is included by default; to add more:
$parameters->set(Option::AUTOLOAD_PATHS, [
// autoload specific file
__DIR__ . '/system/Test/bootstrap.php',
]);

$services = $containerConfigurator->services();
$services->set(UnderscoreToCamelCaseVariableNameRector::class);
};
16 changes: 8 additions & 8 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
*/
public function getClassname(string $file) : string
{
$php = file_get_contents($file);
$tokens = token_get_all($php);
$dlm = false;
$namespace = '';
$class_name = '';
$php = file_get_contents($file);
$tokens = token_get_all($php);
$dlm = false;
$namespace = '';
$className = '';

foreach ($tokens as $i => $token)
{
Expand Down Expand Up @@ -202,17 +202,17 @@ public function getClassname(string $file) : string
&& $tokens[$i - 1][0] === T_WHITESPACE
&& $token[0] === T_STRING)
{
$class_name = $token[1];
$className = $token[1];
break;
}
}

if (empty( $class_name ))
if (empty( $className ))
{
return '';
}

return $namespace . '\\' . $class_name;
return $namespace . '\\' . $className;
}

//--------------------------------------------------------------------
Expand Down
73 changes: 36 additions & 37 deletions system/CLI/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,37 +257,37 @@ public static function input(string $prefix = null): string
*/
public static function prompt(string $field, $options = null, string $validation = null): string
{
$extra_output = '';
$default = '';
$extraOutput = '';
$default = '';

if (is_string($options))
{
$extra_output = ' [' . static::color($options, 'white') . ']';
$default = $options;
$extraOutput = ' [' . static::color($options, 'white') . ']';
$default = $options;
}

if (is_array($options) && $options)
{
$opts = $options;
$extra_output_default = static::color($opts[0], 'white');
$opts = $options;
$extraOutputDefault = static::color($opts[0], 'white');

unset($opts[0]);

if (empty($opts))
{
$extra_output = $extra_output_default;
$extraOutput = $extraOutputDefault;
}
else
{
$extra_output = ' [' . $extra_output_default . ', ' . implode(', ', $opts) . ']';
$validation .= '|in_list[' . implode(',', $options) . ']';
$validation = trim($validation, '|');
$extraOutput = ' [' . $extraOutputDefault . ', ' . implode(', ', $opts) . ']';
$validation .= '|in_list[' . implode(',', $options) . ']';
$validation = trim($validation, '|');
}

$default = $options[0];
}

static::fwrite(STDOUT, $field . $extra_output . ': ');
static::fwrite(STDOUT, $field . $extraOutput . ': ');

// Read the input from keyboard.
$input = trim(static::input()) ?: $default;
Expand Down Expand Up @@ -813,7 +813,6 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
}

//--------------------------------------------------------------------

/**
* Takes a string and writes it to the command line, wrapping to a maximum
* width. If no maximum width is specified, will wrap to the window's max
Expand All @@ -825,11 +824,11 @@ public static function showProgress($thisStep = 1, int $totalSteps = 10)
*
* @param string $string
* @param integer $max
* @param integer $pad_left
* @param integer $padLeft
*
* @return string
*/
public static function wrap(string $string = null, int $max = 0, int $pad_left = 0): string
public static function wrap(string $string = null, int $max = 0, int $padLeft = 0): string
{
if (empty($string))
{
Expand All @@ -846,20 +845,20 @@ public static function wrap(string $string = null, int $max = 0, int $pad_left =
$max = CLI::getWidth();
}

$max = $max - $pad_left;
$max = $max - $padLeft;

$lines = wordwrap($string, $max, PHP_EOL);

if ($pad_left > 0)
if ($padLeft > 0)
{
$lines = explode(PHP_EOL, $lines);

$first = true;

array_walk($lines, function (&$line, $index) use ($pad_left, &$first) {
array_walk($lines, function (&$line, $index) use ($padLeft, &$first) {
if (! $first)
{
$line = str_repeat(' ', $pad_left) . $line;
$line = str_repeat(' ', $padLeft) . $line;
}
else
{
Expand Down Expand Up @@ -1070,45 +1069,45 @@ public static function getOptionString(bool $useLongOpts = false, bool $trim = f
public static function table(array $tbody, array $thead = [])
{
// All the rows in the table will be here until the end
$table_rows = [];
$tableRows = [];

// We need only indexes and not keys
if (! empty($thead))
{
$table_rows[] = array_values($thead);
$tableRows[] = array_values($thead);
}

foreach ($tbody as $tr)
{
$table_rows[] = array_values($tr);
$tableRows[] = array_values($tr);
}

// Yes, it really is necessary to know this count
$total_rows = count($table_rows);
$totalRows = count($tableRows);

// Store all columns lengths
// $all_cols_lengths[row][column] = length
$all_cols_lengths = [];
$allColsLengths = [];

// Store maximum lengths by column
// $max_cols_lengths[column] = length
$max_cols_lengths = [];
$maxColsLengths = [];

// Read row by row and define the longest columns
for ($row = 0; $row < $total_rows; $row ++)
for ($row = 0; $row < $totalRows; $row ++)
{
$column = 0; // Current column index
foreach ($table_rows[$row] as $col)
foreach ($tableRows[$row] as $col)
{
// Sets the size of this column in the current row
$all_cols_lengths[$row][$column] = static::strlen($col);
$allColsLengths[$row][$column] = static::strlen($col);

// If the current column does not have a value among the larger ones
// or the value of this is greater than the existing one
// then, now, this assumes the maximum length
if (! isset($max_cols_lengths[$column]) || $all_cols_lengths[$row][$column] > $max_cols_lengths[$column])
if (! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column])
{
$max_cols_lengths[$column] = $all_cols_lengths[$row][$column];
$maxColsLengths[$column] = $allColsLengths[$row][$column];
}

// We can go check the size of the next column...
Expand All @@ -1118,15 +1117,15 @@ public static function table(array $tbody, array $thead = [])

// Read row by row and add spaces at the end of the columns
// to match the exact column length
for ($row = 0; $row < $total_rows; $row ++)
for ($row = 0; $row < $totalRows; $row ++)
{
$column = 0;
foreach ($table_rows[$row] as $col)
foreach ($tableRows[$row] as $col)
{
$diff = $max_cols_lengths[$column] - static::strlen($col);
$diff = $maxColsLengths[$column] - static::strlen($col);
if ($diff)
{
$table_rows[$row][$column] = $table_rows[$row][$column] . str_repeat(' ', $diff);
$tableRows[$row][$column] = $tableRows[$row][$column] . str_repeat(' ', $diff);
}
$column ++;
}
Expand All @@ -1135,24 +1134,24 @@ public static function table(array $tbody, array $thead = [])
$table = '';

// Joins columns and append the well formatted rows to the table
for ($row = 0; $row < $total_rows; $row ++)
for ($row = 0; $row < $totalRows; $row ++)
{
// Set the table border-top
if ($row === 0)
{
$cols = '+';
foreach ($table_rows[$row] as $col)
foreach ($tableRows[$row] as $col)
{
$cols .= str_repeat('-', static::strlen($col) + 2) . '+';
}
$table .= $cols . PHP_EOL;
}

// Set the columns borders
$table .= '| ' . implode(' | ', $table_rows[$row]) . ' |' . PHP_EOL;
$table .= '| ' . implode(' | ', $tableRows[$row]) . ' |' . PHP_EOL;

// Set the thead and table borders-bottom
if (isset($cols) && ($row === 0 && ! empty($thead) || $row + 1 === $total_rows))
if (isset($cols) && ($row === 0 && ! empty($thead) || $row + 1 === $totalRows))
{
$table .= $cols . PHP_EOL;
}
Expand Down
Loading

0 comments on commit 5425519

Please sign in to comment.