1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yoghi\Bundle\MaddaBundleTest\Utils; |
4
|
|
|
|
5
|
|
|
use SLLH\StyleCIBridge\ConfigBridge; |
6
|
|
|
use Symfony\CS\ConfigurationResolver; |
7
|
|
|
use Symfony\CS\FileCacheManager; |
8
|
|
|
use Symfony\CS\Fixer; |
9
|
|
|
use Symfony\CS\LintManager; |
10
|
|
|
|
11
|
|
|
trait FileCompare |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Compare generated class with expected class into resource dir |
15
|
|
|
* |
16
|
|
|
* @param string $resourcesDir fullPath resources dir |
17
|
|
|
* @param string $namespace namespace of class |
18
|
|
|
* @param string $className class name |
19
|
|
|
* @param string $directoryOutput output directory to compare from |
20
|
|
|
* @param bool $createIfNotExist generate file if not exist equals on genereted one |
21
|
|
|
*/ |
22
|
|
|
private function compareClassPhp($resourcesDir, $namespace, $className, $directoryOutput, $createIfNotExist = false) |
23
|
|
|
{ |
24
|
|
|
$fileExpected = $resourcesDir.'/'.$className.'.php'; |
25
|
|
|
$fileName = $className.'.php'; |
26
|
|
|
$fileActual = $directoryOutput.'/'.$namespace.'/'.$fileName; |
27
|
|
|
|
28
|
|
|
// echo file_get_contents($fileActual); |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** @var \Symfony\CS\Config\Config $config */ |
31
|
|
|
$config = ConfigBridge::create(__DIR__.'/../../'); |
32
|
|
|
|
33
|
|
|
$config->setUsingCache(false); |
34
|
|
|
|
35
|
|
|
$fixer = new Fixer(); |
36
|
|
|
$fixer->registerBuiltInFixers(); |
37
|
|
|
$fixer->registerBuiltInConfigs(); |
38
|
|
|
// $fixer->setLintManager(new LintManager()); |
|
|
|
|
39
|
|
|
$fixer->registerCustomFixers($config->getCustomFixers()); |
40
|
|
|
|
41
|
|
|
$cresolver = new ConfigurationResolver(); |
42
|
|
|
$cresolver->setConfig($config); |
43
|
|
|
$cresolver->setAllFixers($fixer->getFixers()); |
44
|
|
|
$cresolver->setOption('level', 'symfony'); |
45
|
|
|
// $cresolver->setOption('fixers', 'eof_ending,strict_param,short_array_syntax,trailing_spaces,indentation,line_after_namespace,php_closing_tag'); |
|
|
|
|
46
|
|
|
$cresolver->setOption('fixers', 'binary_operator_spaces,blank_line_after_namespace,blank_line_after_opening_tag,blank_line_before_return, |
47
|
|
|
braces,cast_spaces,class_definition,concat_without_spaces,declare_equal_normalize,elseif,encoding, |
48
|
|
|
full_opening_tag,function_declaration,function_typehint_space,hash_to_slash_comment,heredoc_to_nowdoc, |
49
|
|
|
include,lowercase_cast,lowercase_constants,lowercase_keywords,method_argument_space,method_separation, |
50
|
|
|
native_function_casing,new_with_braces,no_alias_functions,no_blank_lines_after_class_opening, |
51
|
|
|
no_blank_lines_after_phpdoc,no_closing_tag,no_empty_phpdoc,no_empty_statement, |
52
|
|
|
no_extra_consecutive_blank_lines,no_leading_import_slash,no_leading_namespace_whitespace, |
53
|
|
|
no_multiline_whitespace_around_double_arrow,no_short_bool_cast,no_singleline_whitespace_before_semicolons, |
54
|
|
|
no_spaces_after_function_name,no_spaces_inside_offset,no_spaces_inside_parenthesis,no_tab_indentation, |
55
|
|
|
no_trailing_comma_in_list_call,no_trailing_comma_in_singleline_array,no_trailing_whitespace, |
56
|
|
|
no_trailing_whitespace_in_comment,no_unneeded_control_parentheses,no_unreachable_default_argument_value, |
57
|
|
|
no_unused_imports,no_whitespace_before_comma_in_array,no_whitespace_in_blank_line,normalize_index_brace, |
58
|
|
|
object_operator_without_whitespace,phpdoc_align,phpdoc_annotation_without_dot,phpdoc_indent, |
59
|
|
|
phpdoc_inline_tag,phpdoc_no_access,phpdoc_no_empty_return,phpdoc_no_package,phpdoc_scalar, |
60
|
|
|
phpdoc_separation,phpdoc_single_line_var_spacing,phpdoc_to_comment,phpdoc_trim, |
61
|
|
|
phpdoc_type_to_var,phpdoc_types,phpdoc_var_without_name,pre_increment,print_to_echo,psr4,self_accessor, |
62
|
|
|
short_scalar_cast,simplified_null_return,single_blank_line_at_eof,single_blank_line_before_namespace, |
63
|
|
|
single_class_element_per_statement,single_import_per_statement,single_line_after_imports,single_quote, |
64
|
|
|
space_after_semicolon,standardize_not_equals,switch_case_semicolon_to_colon,switch_case_space, |
65
|
|
|
ternary_operator_spaces,trailing_comma_in_multiline_array,trim_array_spaces,unalign_double_arrow, |
66
|
|
|
unalign_equals,unary_operator_spaces,unix_line_endings,visibility_required,whitespace_after_comma_in_array, |
67
|
|
|
short_array_syntax,linebreak_after_opening_tag,ordered_imports,no_useless_return,phpdoc_order,ordered_use, |
68
|
|
|
-phpdoc_short_description'); |
69
|
|
|
|
70
|
|
|
$cresolver->resolve(); |
71
|
|
|
|
72
|
|
|
$config->fixers($cresolver->getFixers()); |
73
|
|
|
|
74
|
|
|
// $fileCacheManager = new FileCacheManager(false, $directoryOutput, $cresolver->getFixers()); |
|
|
|
|
75
|
|
|
$iFile = new SplFileInfo($fileActual, $directoryOutput.'/'.$namespace, $fileName); |
76
|
|
|
// $fixer->fixFile($iFile, $cresolver->getFixers(), false, false, $fileCacheManager); |
|
|
|
|
77
|
|
|
|
78
|
|
|
$config->finder(new \ArrayIterator([$iFile])); |
79
|
|
|
|
80
|
|
|
// $changed = |
81
|
|
|
$fixer->fix($config, false, false); |
82
|
|
|
|
83
|
|
|
$fileActualFixed = $iFile->getPathname(); |
84
|
|
|
$actual = file_get_contents($fileActualFixed); |
85
|
|
|
|
86
|
|
|
if (!file_exists($fileExpected) && $createIfNotExist) { |
87
|
|
|
file_put_contents($fileExpected, $actual); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$expected = file_get_contents($fileExpected); |
91
|
|
|
|
92
|
|
|
$this->assertSame($expected, $actual, 'Classe '.$className.' invalid'); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function compareFile($resourcesDir, $directoryOutput, $pathFie, $createIfNotExist = false) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$fileInput = $resourcesDir.'/'.$pathFie; |
98
|
|
|
$actual = file_get_contents($directoryOutput.'/'.$pathFie); |
99
|
|
|
|
100
|
|
|
if (!file_exists($fileInput) && $createIfNotExist) { |
101
|
|
|
file_put_contents($fileInput, $actual); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$expected = file_get_contents($fileInput); |
105
|
|
|
|
106
|
|
|
$this->assertSame($expected, $actual, 'File '.$pathFie.' invalid'); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.