Merge pull request #95 from mlocati/lint-php

Lint PHP files
pull/98/head
Michele Locati 2019-12-27 15:07:33 +01:00 committed by GitHub
commit 266314c301
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 437 additions and 39 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/vendor/
/.php_cs.cache
/composer.lock

329
.php_cs Normal file
View File

@ -0,0 +1,329 @@
<?php
/*
* This document has been generated with
* https://mlocati.github.io/php-cs-fixer-configurator/#version:2.16.1|configurator
* you can change this configuration by importing this file.
*/
return PhpCsFixer\Config::create()
->setUsingCache(false)
->setRiskyAllowed(true)
->setRules([
// Replaces `dirname(__FILE__)` expression with equivalent `__DIR__` constant.
'dir_constant' => true,
// Replace deprecated `ereg` regular expression functions with `preg`.
'ereg_to_preg' => true,
// Error control operator should be added to deprecation notices and/or removed from other cases.
'error_suppression' => true,
// Order the flags in `fopen` calls, `b` and `t` must be last.
'fopen_flag_order' => true,
// The flags in `fopen` calls must omit `t`, and `b` must be omitted or included consistently.
'fopen_flags' => ['b_mode'=>false],
// Replace core functions calls returning constants with the constants.
'function_to_constant' => true,
// Function `implode` must be called with 2 arguments in the documented order.
'implode_call' => true,
// Replaces `is_null($var)` expression with `null === $var`.
'is_null' => true,
// Replaces `intval`, `floatval`, `doubleval`, `strval` and `boolval` function calls with according type casting operator.
'modernize_types_casting' => true,
// Add leading `\` before constant invocation of internal constant to speed up resolving. Constant name match is case-sensitive, except for `null`, `false` and `true`.
'native_constant_invocation' => ['fix_built_in'=>false,'include'=>['DIRECTORY_SEPARATOR','PHP_SAPI','PHP_VERSION_ID'],'scope'=>'namespaced'],
// Add leading `\` before function invocation to speed up resolving.
'native_function_invocation' => ['include'=>['@compiler_optimized'],'scope'=>'namespaced','strict'=>true],
// Master functions shall be used instead of aliases.
'no_alias_functions' => true,
// Replace accidental usage of homoglyphs (non ascii characters) in names.
'no_homoglyph_names' => true,
// Remove Zero-width space (ZWSP), Non-breaking space (NBSP) and other invisible unicode symbols.
'non_printable_character' => true,
// PHPUnit assertion method calls like `->assertSame(true, $foo)` should be written with dedicated method like `->assertTrue($foo)`.
'php_unit_construct' => true,
// Usage of PHPUnit's mock e.g. `->will($this->returnValue(..))` must be replaced by its shorter equivalent such as `->willReturn(...)`.
'php_unit_mock_short_will_return' => true,
// Class names should match the file name.
'psr4' => true,
// Inside class or interface element `self` should be preferred to the class name itself.
'self_accessor' => true,
// Cast shall be used, not `settype`.
'set_type_to_cast' => true,
// PHP arrays should be declared using the configured syntax.
'array_syntax' => ['syntax'=>'short'],
// Binary operators should be surrounded by space as configured.
'binary_operator_spaces' => true,
// There MUST be one blank line after the namespace declaration.
'blank_line_after_namespace' => true,
// Ensure there is no code on the same line as the PHP open tag and it is followed by a blank line.
'blank_line_after_opening_tag' => true,
// An empty line feed must precede any configured statement.
'blank_line_before_statement' => true,
// The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.
'braces' => ['allow_single_line_closure'=>true],
// A single space or none should be between cast and variable.
'cast_spaces' => true,
// Class, trait and interface elements must be separated with one blank line.
'class_attributes_separation' => ['elements'=>['method']],
// Whitespace around the keywords of a class, trait or interfaces definition should be one space.
'class_definition' => ['single_line'=>true],
// Concatenation should be spaced according configuration.
'concat_space' => ['spacing'=>'one'],
// The PHP constants `true`, `false`, and `null` MUST be written using the correct casing.
'constant_case' => true,
// Equal sign in declare statement should be surrounded by spaces or not following configuration.
'declare_equal_normalize' => true,
// The keyword `elseif` should be used instead of `else if` so that all control keywords look like single words.
'elseif' => true,
// PHP code MUST use only UTF-8 without BOM (remove BOM).
'encoding' => true,
// PHP code must use the long `<?php` tags or short-echo `<?=` tags and not other tag variations.
'full_opening_tag' => true,
// Spaces should be properly placed in a function declaration.
'function_declaration' => true,
// Ensure single space between function's argument and its typehint.
'function_typehint_space' => true,
// Include/Require and file path should be divided with a single space. File path should not be placed under brackets.
'include' => true,
// Pre- or post-increment and decrement operators should be used if possible.
'increment_style' => ['style'=>'post'],
// Code MUST use configured indentation type.
'indentation_type' => true,
// All PHP files must use same line ending.
'line_ending' => true,
// Cast should be written in lower case.
'lowercase_cast' => true,
// PHP keywords MUST be in lower case.
'lowercase_keywords' => true,
// Class static references `self`, `static` and `parent` MUST be in lower case.
'lowercase_static_reference' => true,
// Magic constants should be referred to using the correct casing.
'magic_constant_casing' => true,
// Magic method definitions and calls must be using the correct casing.
'magic_method_casing' => true,
// In method arguments and method call, there MUST NOT be a space before each comma and there MUST be one space after each comma. Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.
'method_argument_space' => ['on_multiline'=>'ensure_fully_multiline'],
// Function defined by PHP should be called using the correct casing.
'native_function_casing' => true,
// Native type hints for functions should use the correct case.
'native_function_type_declaration_casing' => true,
// All instances created with new keyword must be followed by braces.
'new_with_braces' => true,
// There should be no empty lines after class opening brace.
'no_blank_lines_after_class_opening' => true,
// There should not be blank lines between docblock and the documented element.
'no_blank_lines_after_phpdoc' => true,
// There must be a comment when fall-through is intentional in a non-empty case body.
'no_break_comment' => true,
// The closing `? >` tag MUST be omitted from files containing only PHP.
'no_closing_tag' => true,
// There should not be any empty comments.
'no_empty_comment' => true,
// There should not be empty PHPDoc blocks.
'no_empty_phpdoc' => true,
// Remove useless semicolon statements.
'no_empty_statement' => true,
// Removes extra blank lines and/or blank lines following configuration.
'no_extra_blank_lines' => ['tokens'=>['break','continue','curly_brace_block','extra','parenthesis_brace_block','return','square_brace_block','throw','use']],
// Remove leading slashes in `use` clauses.
'no_leading_import_slash' => true,
// The namespace declaration line shouldn't contain leading whitespace.
'no_leading_namespace_whitespace' => true,
// Either language construct `print` or `echo` should be used.
'no_mixed_echo_print' => true,
// Operator `=>` should not be surrounded by multi-line whitespaces.
'no_multiline_whitespace_around_double_arrow' => true,
// Short cast `bool` using double exclamation mark should not be used.
'no_short_bool_cast' => true,
// Single-line whitespace before closing semicolon are prohibited.
'no_singleline_whitespace_before_semicolons' => true,
// When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.
'no_spaces_after_function_name' => true,
// There MUST NOT be spaces around offset braces.
'no_spaces_around_offset' => true,
// There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis.
'no_spaces_inside_parenthesis' => true,
// Removes `@param` and `@return` tags that don't provide any useful information.
'no_superfluous_phpdoc_tags' => ['allow_mixed'=>true,'allow_unused_params'=>true],
// Remove trailing commas in list function calls.
'no_trailing_comma_in_list_call' => true,
// PHP single-line arrays should not have trailing comma.
'no_trailing_comma_in_singleline_array' => true,
// Remove trailing whitespace at the end of non-blank lines.
'no_trailing_whitespace' => true,
// There MUST be no trailing spaces inside comment or PHPDoc.
'no_trailing_whitespace_in_comment' => true,
// Removes unneeded parentheses around control statements.
'no_unneeded_control_parentheses' => true,
// Removes unneeded curly braces that are superfluous and aren't part of a control structure's body.
'no_unneeded_curly_braces' => true,
// A final class must not have final methods.
'no_unneeded_final_method' => true,
// Unused `use` statements must be removed.
'no_unused_imports' => true,
// In array declaration, there MUST NOT be a whitespace before each comma.
'no_whitespace_before_comma_in_array' => true,
// Remove trailing whitespace at the end of blank lines.
'no_whitespace_in_blank_line' => true,
// Array index should always be written by using square braces.
'normalize_index_brace' => true,
// There should not be space before or after object `T_OBJECT_OPERATOR` `->`.
'object_operator_without_whitespace' => true,
// Ordering `use` statements.
'ordered_imports' => true,
// PHPUnit annotations should be a FQCNs including a root namespace.
'php_unit_fqcn_annotation' => true,
// All items of the given phpdoc tags must be either left-aligned or (by default) aligned vertically.
'phpdoc_align' => ['tags'=>['method','param','property','return','throws','type','var']],
// PHPDoc annotation descriptions should not be a sentence.
'phpdoc_annotation_without_dot' => true,
// Docblocks should have the same indentation as the documented subject.
'phpdoc_indent' => true,
// Fix PHPDoc inline tags, make `@inheritdoc` always inline.
'phpdoc_inline_tag' => true,
// `@access` annotations should be omitted from PHPDoc.
'phpdoc_no_access' => true,
// No alias PHPDoc tags should be used.
'phpdoc_no_alias_tag' => true,
// `@package` and `@subpackage` annotations should be omitted from PHPDoc.
'phpdoc_no_package' => true,
// Classy that does not inherit must not have `@inheritdoc` tags.
'phpdoc_no_useless_inheritdoc' => true,
// The type of `@return` annotations of methods returning a reference to itself must the configured one.
'phpdoc_return_self_reference' => true,
// Scalar types should always be written in the same form. `int` not `integer`, `bool` not `boolean`, `float` not `real` or `double`.
'phpdoc_scalar' => true,
// Annotations in PHPDoc should be grouped together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line.
'phpdoc_separation' => true,
// Single line `@var` PHPDoc should have proper spacing.
'phpdoc_single_line_var_spacing' => true,
// PHPDoc summary should end in either a full stop, exclamation mark, or question mark.
'phpdoc_summary' => true,
// Docblocks should only be used on structural elements.
'phpdoc_to_comment' => true,
// PHPDoc should start and end with content, excluding the very first and last line of the docblocks.
'phpdoc_trim' => true,
// Removes extra blank lines after summary and after description in PHPDoc.
'phpdoc_trim_consecutive_blank_line_separation' => true,
// The correct case must be used for standard PHP types in PHPDoc.
'phpdoc_types' => true,
// Sorts PHPDoc types.
'phpdoc_types_order' => true,
// `@var` and `@type` annotations should not contain the variable name.
'phpdoc_var_without_name' => true,
// There should be one or no space before colon, and one space after it in return type declarations, according to configuration.
'return_type_declaration' => true,
// Instructions must be terminated with a semicolon.
'semicolon_after_instruction' => true,
// Cast `(boolean)` and `(integer)` should be written as `(bool)` and `(int)`, `(double)` and `(real)` as `(float)`, `(binary)` as `(string)`.
'short_scalar_cast' => true,
// A PHP file without end tag must always end with a single empty line feed.
'single_blank_line_at_eof' => true,
// There should be exactly one blank line before a namespace declaration.
'single_blank_line_before_namespace' => true,
// There MUST NOT be more than one property or constant declared per statement.
'single_class_element_per_statement' => true,
// There MUST be one use keyword per declaration.
'single_import_per_statement' => true,
// Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block.
'single_line_after_imports' => true,
// Single-line comments and multi-line comments with only one line of actual content should use the `//` syntax.
'single_line_comment_style' => true,
// Throwing exception must be done in single line.
'single_line_throw' => true,
// Convert double quotes to single quotes for simple strings.
'single_quote' => true,
// Each trait `use` must be done as single statement.
'single_trait_insert_per_statement' => true,
// Fix whitespace after a semicolon.
'space_after_semicolon' => ['remove_in_empty_for_expressions'=>true],
// Increment and decrement operators should be used if possible.
'standardize_increment' => true,
// Replace all `<>` with `!=`.
'standardize_not_equals' => true,
// A case should be followed by a colon and not a semicolon.
'switch_case_semicolon_to_colon' => true,
// Removes extra spaces between colon and case value.
'switch_case_space' => true,
// Standardize spaces around ternary operator.
'ternary_operator_spaces' => true,
// PHP multi-line arrays should have a trailing comma.
'trailing_comma_in_multiline_array' => true,
// Arrays should be formatted like function/method arguments, without leading or trailing single line space.
'trim_array_spaces' => true,
// Unary operators should be placed adjacent to their operands.
'unary_operator_spaces' => true,
// Visibility MUST be declared on all properties and methods; `abstract` and `final` MUST be declared before the visibility; `static` MUST be declared after the visibility.
'visibility_required' => true,
// In array declaration, there MUST be a whitespace after each comma.
'whitespace_after_comma_in_array' => true,
// Each line of multi-line DocComments must have an asterisk [PSR-5] and must be aligned with the first one.
'align_multiline_comment' => true,
// Each element of an array must be indented exactly once.
'array_indentation' => true,
// Using `isset($var) &&` multiple times should be done in one call.
'combine_consecutive_issets' => true,
// Calling `unset` on multiple items should be done in one call.
'combine_consecutive_unsets' => true,
// Remove extra spaces in a nullable typehint.
'compact_nullable_typehint' => true,
// Escape implicit backslashes in strings and heredocs to ease the understanding of which are special chars interpreted by PHP and which not.
'escape_implicit_backslashes' => true,
// Add curly braces to indirect variables to make them clear to understand. Requires PHP >= 7.0.
'explicit_indirect_variable' => true,
// Converts implicit variables into explicit ones in double-quoted strings or heredoc syntax.
'explicit_string_variable' => true,
// Transforms imported FQCN parameters and return types in function arguments to short version.
'fully_qualified_strict_types' => true,
// Convert `heredoc` to `nowdoc` where possible.
'heredoc_to_nowdoc' => true,
// Method chaining MUST be properly indented. Method chaining with different levels of indentation is not supported.
'method_chaining_indentation' => true,
// DocBlocks must start with two asterisks, multiline comments must start with a single asterisk, after the opening slash. Both must end with a single asterisk before the closing slash.
'multiline_comment_opening_closing' => true,
// Forbid multi-line whitespace before the closing semicolon or move the semicolon to the new line for chained calls.
'multiline_whitespace_before_semicolons' => ['strategy'=>'new_line_for_chained_calls'],
// Replace control structure alternative syntax to use braces.
'no_alternative_syntax' => true,
// There should not be a binary flag before strings.
'no_binary_string' => true,
// Properties MUST not be explicitly initialized with `null` except when they have a type declaration (PHP 7.4).
'no_null_property_initialization' => true,
// Replace short-echo `<?=` with long format `<?php echo` syntax.
'no_short_echo_tag' => true,
// Replaces superfluous `elseif` with `if`.
'no_superfluous_elseif' => true,
// Variables must be set `null` instead of using `(unset)` casting.
'no_unset_cast' => true,
// There should not be useless `else` cases.
'no_useless_else' => true,
// There should not be an empty `return` statement at the end of a function.
'no_useless_return' => true,
// Orders the elements of classes/interfaces/traits.
'ordered_class_elements' => true,
// All PHPUnit test classes should be marked as internal.
'php_unit_internal_class' => true,
// Enforce camel (or snake) case for PHPUnit test methods, following configuration.
'php_unit_method_casing' => true,
// Order `@covers` annotation of PHPUnit tests.
'php_unit_ordered_covers' => true,
// Adds a default `@coversNothing` annotation to PHPUnit test classes that have no `@covers*` annotation.
'php_unit_test_class_requires_covers' => true,
// PHPDoc should contain `@param` for all params.
'phpdoc_add_missing_param_annotation' => true,
// `@return void` and `@return null` annotations should be omitted from PHPDoc.
'phpdoc_no_empty_return' => true,
// Annotations in PHPDoc should be ordered so that `@param` annotations come first, then `@throws` annotations, then `@return` annotations.
'phpdoc_order' => true,
// `@var` and `@type` annotations must have type and name in the correct order.
'phpdoc_var_annotation_correct_order' => true,
// Converts `protected` variables and methods to `private` where possible.
'protected_to_private' => true,
// Local, dynamic and directly referenced variables should not be assigned and directly returned by a function or method.
'return_assignment' => true,
// Converts explicit variables in double-quoted strings and heredoc syntax from simple to complex format (`${` to `{$`).
'simple_to_complex_string_variable' => true,
])
->setFinder(PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
->name('.php_cs')
)
;

View File

@ -19,13 +19,21 @@ test-extensions-job-template: &test-extensions-job-template
jobs:
include:
- stage: Pre-test checks
name: Check coding style
name: Check shell coding style
if: type = pull_request
language: go
go: '1.13'
before_script: GO111MODULE=on go install mvdan.cc/sh/v3/cmd/shfmt
script: ./scripts/invoke-shfmt check
- stage: Pre-test checks
name: Check PHP coding style
if: type = pull_request
language: php
php: '7.4'
install: composer install -a
script: composer run-script lint -- --dry-run --diff
- <<: *test-extensions-job-template
name: Test extensions on Alpine 3.7
env: DOCKER_DISTRO=alpine3.7

19
composer.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "mlocati/docker-php-extension-installer",
"description": "Easily install PHP extension in Docker containers",
"type": "project",
"license": "MIT",
"authors": [
{
"name": "Michele Locati",
"email": "michele@locati.it"
}
],
"minimum-stability": "stable",
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16"
},
"scripts": {
"lint": "php-cs-fixer fix --path-mode=intersection --config=.php_cs ."
}
}

View File

@ -2,9 +2,9 @@
$rc = 0;
$numTestedExtensions = 0;
$nameMap = array(
$nameMap = [
'opcache' => 'Zend OPcache',
);
];
$testsDir = __DIR__ . '/tests';
function runTest($testFile)
{
@ -25,12 +25,12 @@ for ($index = 1, $count = isset($argv) ? count($argv) : 0; $index < $count; $ind
if (!extension_loaded($extension)) {
fprintf(STDERR, sprintf("Extension not loaded: %s\n", $extension));
} else {
$testFile = "${testsDir}/${extension}.php";
$testFile = "{$testsDir}/{$extension}.php";
if (is_file($testFile)) {
try {
if (runTest($testFile) === true) {
fprintf(STDOUT, sprintf("Extension tested successfully: %s\n", $extension));
$rcThis = 0;
$rcThis = 0;
} else {
fprintf(STDERR, sprintf("Extension test failed: %s\n", $extension));
}

View File

@ -3,14 +3,28 @@
set -o errexit
set -o nounset
SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. && pwd)"
cd "$SRC_DIR"
rc=0
echo '# Linting shell scripts'
if ! docker --version >/dev/null 2>/dev/null; then
printf 'Docker is not installed, or it is not running\n' >&2
exit 1
echo 'Docker is not installed, or it is not running.' >&2
rc=1
elif ! docker run --rm -v "$SRC_DIR:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix; then
echo 'ERROR!' >&2
rc=1
fi
SRC_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && cd .. pwd)"
if ! docker run --rm -v "$SRC_DIR:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix; then
printf 'ERROR!' >&2
exit 1
echo '# Linting PHP files'
if ! composer --version >/dev/null 2>/dev/null; then
echo 'Composer is not installed.' >&2
elif ! test -f ./vendor/autoload.php; then
echo 'Composer dependencies are not installed.' >&2
rc=1
elif ! composer run-script lint; then
echo 'ERROR!' >&2
rc=1
fi
exit $rc

View File

@ -1,21 +1,44 @@
@echo off
setlocal
docker --version >NUL 2>NUL
if errorlevel 1 goto :no-docker
cd /D "%~dp0.."
if errorlevel 1 (
echo ERROR! >&2
exit /B 1
)
cd /d "%~dp0.."
if errorlevel 1 goto err
set SRC_DIR=%CD%
set rc=0
docker run --rm -v "%SRC_DIR%:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix
if errorlevel 1 goto :err
goto :eof
echo # Linting shell scripts
docker --version >NUL 2>NUL
if errorlevel 1 (
echo Docker is not installed, or it's not running >&2
set rc=1
) else (
docker run --rm -v "%SRC_DIR%:/src" -w /src --entrypoint /src/scripts/invoke-shfmt mvdan/shfmt:latest fix
if errorlevel 1 (
echo ERROR! >&2
set rc=1
)
)
:no-docker
echo Docker is not installed, or it's not running >&2
goto :eof
echo # Linting PHP files
call composer --version >NUL 2>NUL
if errorlevel 1 (
echo Composer is not installed. >&2
set rc=1
) else (
if not exist .\vendor\autoload.php (
echo Composer dependencies are not installed. >&2
set rc=1
) else (
call composer run-script lint
if errorlevel 1 (
echo ERROR! >&2
set rc=1
)
)
)
:err
echo ERROR! >&2
goto :eof
exit /B %rc%

View File

@ -3,7 +3,7 @@
$imageWidth = 8;
$imageHeight = 16;
$image = imagecreatetruecolor($imageWidth, $imageHeight);
$formats = array(
$formats = [
'gd2',
'gif',
'jpeg',
@ -14,39 +14,40 @@ $formats = array(
'xbm',
'xpm',
'gd',
);
];
if (PHP_VERSION_ID >= 70200) {
$formats = array_merge($formats, array(
$formats = array_merge($formats, [
'bmp',
));
]);
}
$tempFile = null;
$image2 = null;
try {
foreach ($formats as $format) {
$loadFuntion = "imagecreatefrom${format}";
$loadFuntion = "imagecreatefrom{$format}";
if (!function_exists($loadFuntion)) {
throw new Exception("$loadFuntion() function is missing");
throw new Exception("{$loadFuntion}() function is missing");
}
if ($format === 'xpm') {
continue;
}
$saveFuntion = "image${format}";
$saveFuntion = "image{$format}";
if (!function_exists($saveFuntion)) {
throw new Exception("$saveFuntion() function is missing");
throw new Exception("{$saveFuntion}() function is missing");
}
$tempFile = tempnam(sys_get_temp_dir(), 'dpei');
if ($saveFuntion($image, $tempFile) === false) {
throw new Exception("$saveFuntion() failed");
throw new Exception("{$saveFuntion}() failed");
}
if (!is_file($tempFile) || filesize($tempFile) < 1) {
throw new Exception("$saveFuntion() created an empty file");
throw new Exception("{$saveFuntion}() created an empty file");
}
$image2 = $loadFuntion($tempFile);
unlink($tempFile);
$tempFile = null;
if (!is_resource($image2) || imagesx($image2) !== $imageWidth || imagesy($image2) !== $imageHeight) {
throw new Exception("$loadFuntion() failed");
throw new Exception("{$loadFuntion}() failed");
}
imagedestroy($image2);
}
@ -55,15 +56,16 @@ try {
if (is_resource($image2)) {
imagedestroy($image2);
}
if($tempFile !== null) {
if ($tempFile !== null) {
unlink($tempFile);
}
}
if (!function_exists('imagefttext')) {
throw new Exception("imagefttext() function is missing");
throw new Exception('imagefttext() function is missing');
}
if (!function_exists('imageantialias')) {
throw new Exception("imageantialias() function is missing");
throw new Exception('imageantialias() function is missing');
}
return true;