Conditions | 2 |
Paths | 2 |
Total Lines | 88 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | public function __invoke(string $name, InputInterface $input, OutputInterface $output) |
||
19 | { |
||
20 | $output->writeln('<info># Installing Code Sniffer...</info>'); |
||
21 | |||
22 | $directory = getcwd() . '/' . $name; |
||
23 | |||
24 | $composer = $this->findComposer(); |
||
25 | |||
26 | $commands = [ |
||
27 | $composer . ' require --dev squizlabs/php_codesniffer', |
||
28 | ]; |
||
29 | |||
30 | $runner = new CommandRunner($output, $directory); |
||
31 | $runner->run($commands); |
||
32 | |||
33 | $filesystem = new ProjectFilesystem(getcwd() . '/' . $name); |
||
34 | |||
35 | $filesystem->createFile( |
||
36 | 'phpcs.xml.dist', |
||
37 | <<<FILE |
||
38 | <?xml version="1.0"?> |
||
39 | <ruleset name="{$name}"> |
||
40 | <description>The coding standard of {$name}</description> |
||
41 | |||
42 | <file>app</file> |
||
43 | <file>config</file> |
||
44 | <file>database</file> |
||
45 | <file>routes</file> |
||
46 | <file>tests</file> |
||
47 | |||
48 | <arg value="p" /> |
||
49 | |||
50 | <config name="ignore_warnings_on_exit" value="1" /> |
||
51 | <config name="ignore_errors_on_exit" value="1" /> |
||
52 | <config name="php_version" value="70205" /> |
||
53 | |||
54 | <arg name="basepath" value="."/> |
||
55 | <arg name="extensions" value="php" /> |
||
56 | <arg name="colors" /> |
||
57 | <arg value="s" /> |
||
58 | |||
59 | <!-- Use the PSR12 Standard--> |
||
60 | <rule ref="PSR12"> |
||
61 | <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/> |
||
62 | </rule> |
||
63 | |||
64 | <!-- Ban some functions --> |
||
65 | <rule ref="Generic.PHP.ForbiddenFunctions"> |
||
66 | <properties> |
||
67 | <property name="forbiddenFunctions" type="array"> |
||
68 | <element key="sizeof" value="count"/> |
||
69 | <element key="delete" value="unset"/> |
||
70 | <element key="print" value="echo"/> |
||
71 | <element key="is_null" value="null"/> |
||
72 | <element key="create_function" value="null"/> |
||
73 | </property> |
||
74 | </properties> |
||
75 | </rule> |
||
76 | </ruleset> |
||
77 | FILE |
||
78 | ); |
||
79 | |||
80 | $output->writeln('<info>Update file: composer.json</info>'); |
||
81 | $filesystem->updateFile('composer.json', function (string $content) { |
||
82 | $refreshScript = <<<STRING |
||
83 | "test": "phpunit", |
||
84 | "check-style": "phpcs", |
||
85 | "fix-style": "phpcbf; if [ $? -eq 1 ]; then exit 0; fi", |
||
86 | |||
87 | STRING; |
||
88 | $patterns = [ |
||
89 | '/("scripts": {\n)/', |
||
90 | ]; |
||
91 | $replace = [ |
||
92 | '${1}' . $refreshScript, |
||
93 | ]; |
||
94 | |||
95 | return preg_replace($patterns, $replace, $content); |
||
96 | }); |
||
97 | |||
98 | |||
99 | $isSuccessful = $runner->run([ |
||
100 | $composer . ' fix-style', |
||
101 | $composer . ' check-style', |
||
102 | ]); |
||
103 | |||
104 | if ($isSuccessful) { |
||
105 | $output->writeln('<comment>Code Sniffer installed.</comment>'); |
||
106 | } |
||
125 |