Skip to content

Commit

Permalink
fix cs
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev authored and xtrime-ru committed Jul 17, 2023
1 parent ac5f334 commit 404f83a
Show file tree
Hide file tree
Showing 38 changed files with 640 additions and 112 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.2.8","version":"3.22.0:v3.22.0#92b019f6c8d79aa26349d0db7671d37440dc0ff3","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"class_definition":{"single_line":true},"compact_nullable_typehint":true,"curly_braces_position":{"allow_single_line_anonymous_functions":true,"allow_single_line_empty_anonymous_classes":true},"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_line_after_imports":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"doctrine_annotation_array_assignment":{"operator":":"},"doctrine_annotation_braces":true,"doctrine_annotation_indentation":true,"doctrine_annotation_spaces":{"before_array_assignments_colon":false},"align_multiline_comment":true,"array_syntax":{"syntax":"short"},"backtick_to_shell_exec":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"cast_spaces":true,"class_attributes_separation":{"elements":{"const":"one","method":"one","property":"one"}},"class_reference_name_casing":true,"clean_namespace":true,"concat_space":true,"declare_parentheses":true,"echo_tag_syntax":true,"empty_loop_body":{"style":"braces"},"empty_loop_condition":true,"fully_qualified_strict_types":true,"general_phpdoc_tag_rename":{"replacements":{"inheritDocs":"inheritDoc"}},"global_namespace_import":{"import_classes":false,"import_constants":false,"import_functions":false},"include":true,"increment_style":true,"integer_literal_case":true,"lambda_not_used_import":true,"linebreak_after_opening_tag":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"no_alias_language_construct_call":true,"no_alternative_syntax":true,"no_binary_string":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["attribute","case","continue","curly_brace_block","default","extra","parenthesis_brace_block","square_brace_block","switch","throw","use"]},"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_null_property_initialization":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_superfluous_phpdoc_tags":{"remove_inheritdoc":true},"no_trailing_comma_in_singleline":true,"no_unneeded_control_parentheses":{"statements":["break","clone","continue","echo_print","others","return","switch_case","yield","yield_from"]},"no_unneeded_curly_braces":{"namespaces":true},"no_unneeded_import_alias":true,"no_unset_cast":true,"no_unused_imports":true,"no_useless_concat_operator":true,"no_useless_nullsafe_operator":true,"no_whitespace_before_comma_in_array":true,"normalize_index_brace":true,"nullable_type_declaration_for_default_null_value":{"use_nullable_type_declaration":false},"object_operator_without_whitespace":true,"operator_linebreak":{"only_booleans":true},"php_unit_fqcn_annotation":true,"php_unit_method_casing":true,"phpdoc_align":true,"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag_normalizer":true,"phpdoc_no_access":true,"phpdoc_no_alias_tag":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_order":{"order":["param","return","throws"]},"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_tag_type":{"tags":{"inheritDoc":"inline"}},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"semicolon_after_instruction":true,"simple_to_complex_string_variable":true,"single_line_comment_spacing":true,"single_line_comment_style":{"comment_types":["hash"]},"single_line_throw":true,"single_quote":true,"single_space_around_construct":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"switch_continue_to_break":true,"trailing_comma_in_multiline":true,"trim_array_spaces":true,"type_declaration_spaces":true,"types_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":true,"array_push":true,"combine_nested_dirname":true,"dir_constant":true,"ereg_to_preg":true,"error_suppression":true,"fopen_flag_order":true,"fopen_flags":{"b_mode":false},"function_to_constant":true,"get_class_to_class_keyword":true,"implode_call":true,"is_null":true,"logical_operators":true,"modernize_strpos":true,"native_constant_invocation":{"strict":false},"native_function_invocation":{"include":["@compiler_optimized"],"scope":"namespaced","strict":true},"no_alias_functions":true,"no_homoglyph_names":true,"no_php4_constructor":true,"no_unneeded_final_method":true,"no_useless_sprintf":true,"non_printable_character":true,"ordered_traits":true,"php_unit_construct":true,"php_unit_mock_short_will_return":true,"php_unit_set_up_tear_down_visibility":true,"php_unit_test_annotation":true,"psr_autoloading":true,"self_accessor":true,"set_type_to_cast":true,"string_length_to_empty":true,"string_line_ending":true,"ternary_to_elvis_operator":true,"pow_to_exponentiation":true,"no_trailing_whitespace_in_string":true,"array_indentation":true,"not_operator_with_successor_space":true},"hashes":{"src\/Traits\/AliasesEvaluationMethods.php":"c2c204190c9f8ee16c92ef770adba3d7","src\/Traits\/AliasesSelectionMethods.php":"5ced4ff7816eb01a022d45305ef99ba6","src\/Puppeteer.php":"ebb56c27128f660040b4fc4dcb92dfd2","src\/Command\/GenerateDocumentationCommand.php":"e003aa6213a763e475b559bf9288818e","src\/Resources\/Page.php":"2b0039c3e9ead7aea5b86660d58fa34f","src\/Resources\/HTTPResponse.php":"3e731adab3caa902f3ad7cd8fdb39974","src\/Resources\/Target.php":"12d767b6c699a87ebcfbe69d50056d2c","src\/Resources\/Accessibility.php":"6e2e1d9549138006a975f7e55096028a","src\/Resources\/HTTPRequest.php":"429c11ffdcf6a5f26286f3cb2653471d","src\/Resources\/Tracing.php":"9f6625ad696d871f63f3d3d34fdf8f01","src\/Resources\/Keyboard.php":"349acb30ee7ad560ec6370f4ed8140e2","src\/Resources\/BrowserFetcher.php":"93f9f8d0379d7d39aaeec1c26c1963d3","src\/Resources\/Browser.php":"ff656e3b54fee6c30ce9ae82986e3528","src\/Resources\/ConsoleMessage.php":"a1ae759076e6e0147152171c9be29635","src\/Resources\/ExecutionContext.php":"8e6988e60eb2ddc48964cc199dfedab1","src\/Resources\/Dialog.php":"b2ece20124c52e93cf16c43214696105","src\/Resources\/BrowserContext.php":"133977746f8c1596a3d3c93d9533f843","src\/Resources\/EventEmitter.php":"65c99d871654f61624df460d488bd2ad","src\/Resources\/Coverage.php":"e222ecf5d77cffbd18d5ac5c89e0ab10","src\/Resources\/SecurityDetails.php":"819b7ba667dde8a6433fa62ea9e64cd8","src\/Resources\/CDPSession.php":"e999386c41f830b9431a53ac908b94d6","src\/Resources\/FileChooser.php":"ef46abd1a4230ba87fc590bcb2cc6390","src\/Resources\/PageTarget.php":"3f870625218d84225db104dcb0b63970","src\/Resources\/Touchscreen.php":"f4d69f06e26b17a6c00a4cae65db7257","src\/Resources\/CDPSessionImpl.php":"b7883dc60dcb946ec7780c926b5e18f8","src\/Resources\/TimeoutError.php":"90aa6bba93773c71a87d07b7a6c1c1c1","src\/Resources\/Frame.php":"6129065a9c8d65270f3df8e367669820","src\/Resources\/ElementHandle.php":"ab08ae85b1f7845355471590b34107ca","src\/Resources\/JSHandle.php":"015d94134296d80ca4c6325418c965ca","src\/Resources\/WebWorker.php":"2a66cb9faac094f74b3ebb280cebf768","src\/Resources\/Mouse.php":"41cb1a98dcc8d438e0ff25fb8a6cc689","src\/PuppeteerProcessDelegate.php":"5d19d2d6b3b620ad9188d7d59f90a640"}}
2 changes: 1 addition & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$finder = Symfony\Component\Finder\Finder::create()
->in([
__DIR__ . 'src'
__DIR__ . '/src'
])
->name('*.php')
->ignoreDotFiles(true)
Expand Down
15 changes: 9 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@
}
],
"require": {
"php": "^7.3 || ^8.0",
"php": "^8.0",
"ext-json": "*",
"composer/semver": "^3.0",
"nesk/rialto": "dev-zoon",
"psr/log": "^1.0 || ^2.0 || ^3.0"
"psr/log": "^3.0",
"thecodingmachine/safe": "^2.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.2",
"monolog/monolog": "^2.0|^3.0",
"monolog/monolog": "^3.0",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0",
"symfony/console": "^4.0|^5.0|^6.0",
"symfony/process": "^4.0|^5.0|^6.0",
"symfony/var-dumper": "^6.3"
"symfony/console": "^6.3",
"symfony/filesystem": "^6.3",
"symfony/process": "^6.3",
"symfony/var-dumper": "^6.3",
"thecodingmachine/phpstan-safe-rule": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ parameters:
paths:
- src
fileExtensions:
- php
- php
includes:
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
59 changes: 34 additions & 25 deletions src/Command/GenerateDocumentationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,29 @@
namespace Nesk\Puphpeteer\Command;

use Nesk\Puphpeteer\Puppeteer;
use Symfony\Component\Process\Process;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Process;

final class GenerateDocumentationCommand extends Command
{
private const DOC_FILE_NAME = 'doc-generator';

private const BUILD_DIR = __DIR__.'/../../.build';

private const NODE_MODULES_DIR = __DIR__.'/../../node_modules';

private const RESOURCES_DIR = __DIR__.'/../Resources';

private const RESOURCES_NAMESPACE = 'Nesk\\Puphpeteer\\Resources';

private const DOC_FORMAT_PHP = 'php';

private const DOC_FORMAT_PHPSTAN = 'phpstan';

private const DOC_FORMATS = [self::DOC_FORMAT_PHP, self::DOC_FORMAT_PHPSTAN];

protected static $defaultName = 'doc:generate';
Expand Down Expand Up @@ -56,13 +63,13 @@ private static function getDocumentation(string $puppeteerPath, array $resourceN
{
self::buildDocumentationGenerator();

$commonFiles = \glob("$puppeteerPath/lib/esm/puppeteer/common/*.d.ts");
$nodeFiles = \glob("$puppeteerPath/lib/esm/puppeteer/node/*.d.ts");
$commonFiles = \Safe\glob("$puppeteerPath/lib/esm/puppeteer/common/*.d.ts");
$nodeFiles = \Safe\glob("$puppeteerPath/lib/esm/puppeteer/node/*.d.ts");

$result = [];
foreach(self::DOC_FORMATS as $format) {
foreach (self::DOC_FORMATS as $format) {
$process = new Process(
\array_merge(
array_merge(
['node', self::BUILD_DIR.'/'.self::DOC_FILE_NAME.'.js', $format],
$commonFiles,
$nodeFiles,
Expand All @@ -72,7 +79,7 @@ private static function getDocumentation(string $puppeteerPath, array $resourceN
);
$process->mustRun();

foreach (\json_decode($process->getOutput(), true) as &$class) {
foreach (\Safe\json_decode($process->getOutput(), true) as &$class) {
$result[$class['name']]['name'] = $class['name'];
$result[$class['name']][$format] = [
'properties' => $class['properties'],
Expand All @@ -88,33 +95,33 @@ private static function getDocumentation(string $puppeteerPath, array $resourceN
private static function getResourceNames(): array
{
return array_map(static function (string $filePath): string {
return explode('.', \basename($filePath))[0];
}, \glob(self::RESOURCES_DIR.'/*'));
return explode('.', basename($filePath))[0];
}, glob(self::RESOURCES_DIR.'/*'));
}

private static function generatePhpDocWithDocumentation(array $classDocumentation): ?string
{
$properties = array_map(function (string $property): string {
return "\n * @property $property";
}, $classDocumentation[self::DOC_FORMAT_PHP]['properties']);
$properties = \implode('', $properties);
$properties = implode('', $properties);

$getters = array_map(function (string $getter): string {
return "\n * @property-read $getter";
}, $classDocumentation[self::DOC_FORMAT_PHP]['getters']);
$getters = \implode('', $getters);
$getters = implode('', $getters);

$methods = '';
foreach ($classDocumentation[self::DOC_FORMAT_PHP]['methods'] as $pos => $method) {
$methods .= "\n * @method $method";

$phpStanMethod = $classDocumentation[self::DOC_FORMAT_PHPSTAN]['methods'][$pos];
//phpStorm works incorrectly if @phpstan-method is used.
//Using non-standard method-extended phpDoc:
// phpStorm works incorrectly if @phpstan-method is used.
// Using non-standard method-extended phpDoc:
$methods .= "\n * @method-extended $phpStanMethod";
}

if (\strlen($properties) > 0 || \strlen($getters) > 0 || \strlen($methods) > 0) {
if ('' !== $properties || '' !== $getters || '' !== $methods) {
return "/**$properties$getters$methods\n */";
}

Expand Down Expand Up @@ -144,8 +151,8 @@ private static function writePhpDoc(string $className, string $phpDoc): void

$lines = explode("\n", $contents);

$before = array_slice($lines, 0, $startLine - 1);
$after = array_slice($lines, $startLine - 1);
$before = \array_slice($lines, 0, $startLine - 1);
$after = \array_slice($lines, $startLine - 1);

$newContents = implode("\n", array_merge($before, explode("\n", $phpDoc), $after));
}
Expand All @@ -166,9 +173,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($resourceNames as $resourceName) {
$classDocumentation = $documentation[$resourceName] ?? null;

if ($classDocumentation !== null) {
if (null !== $classDocumentation) {
$phpDoc = self::generatePhpDocWithDocumentation($classDocumentation);
if ($phpDoc !== null) {
if (null !== $phpDoc) {
$resourceClass = self::RESOURCES_NAMESPACE.'\\'.$resourceName;
self::writePhpDoc($resourceClass, $phpDoc);
}
Expand All @@ -178,35 +185,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// Handle the specific Puppeteer class
$classDocumentation = array_replace_recursive($documentation['Puppeteer'], $documentation['PuppeteerNode']);
unset($documentation['Puppeteer'], $documentation['PuppeteerNode']);
if ($classDocumentation !== null) {
if (null !== $classDocumentation) {
$phpDoc = self::generatePhpDocWithDocumentation($classDocumentation);
if ($phpDoc !== null) {
if (null !== $phpDoc) {
self::writePhpDoc(Puppeteer::class, $phpDoc);
}
}

$missingResources = \array_diff(\array_keys($documentation), $resourceNames);
$missingResources = array_diff(array_keys($documentation), $resourceNames);
foreach ($missingResources as $resource) {
$io->warning("The $resource class in Puppeteer doesn't have any equivalent in PuPHPeteer.");
}

$inexistantResources = \array_diff($resourceNames, \array_keys($documentation));
$inexistantResources = array_diff($resourceNames, array_keys($documentation));
foreach ($inexistantResources as $resource) {
$io->error("The $resource resource doesn't have any equivalent in Puppeteer.");
}

return 0;
}

private static function rmdirRecursive(string $dir): bool {
private static function rmdirRecursive(string $dir): bool
{
$files = scandir($dir);
if (!is_array($files)) {
if (! \is_array($files)) {
return false;
}
$files = array_diff($files, array('.','..'));
$files = array_diff($files, ['.', '..']);
foreach ($files as $file) {
(is_dir("$dir/$file")) ? self::rmdirRecursive("$dir/$file") : unlink("$dir/$file");
}

return rmdir($dir);
}
}
Loading

0 comments on commit 404f83a

Please sign in to comment.