Skip to content

Commit 0f7d4f5

Browse files
committed
Merge pull request #24 in PLUG_OPEN/frontend_swagcustomsort from hack-time/refactoring-improvements to master
* commit '7fefb2220517bb3783042331a39e8e65944eb4ec': Fix code style; Improve code; Rename article occurences to product Add composer.json Add cs-fixer and fix code style
2 parents b875da2 + 7fefb22 commit 0f7d4f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1157
-968
lines changed

.eslintignore

Whitespace-only changes.

.githooks/install_hooks.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
exec("ln -s ../../.githooks/pre-commit ".__DIR__."/../.git/hooks/pre-commit");
5+
exec("chmod +x ".__DIR__ . "/../.git/hooks/pre-commit");

.githooks/pre-commit

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
/**
5+
* .git/hooks/pre-commit
6+
*
7+
* This pre-commit hooks will check for PHP errors (lint), and make sure the
8+
* code is PSR-2 compliant.
9+
*/
10+
class PreCommitChecks
11+
{
12+
/**
13+
* @var bool
14+
*/
15+
private $error = false;
16+
17+
/**
18+
* @return int
19+
*/
20+
public function run()
21+
{
22+
$this->writeln();
23+
$this->writeln('Checking commit requirements', 0);
24+
$this->writeln();
25+
26+
if ($this->isRebase()) {
27+
echo 'Not on branch' . PHP_EOL;
28+
29+
return 0;
30+
}
31+
32+
$this->runPhpLint($this->getCommittedFileList());
33+
$this->runPhpCsFixer($this->getCommittedFileList());
34+
$this->runEsLint($this->getCommittedFileList('js'));
35+
36+
if ($this->error) {
37+
$this->writeln("If you are ABSOLUTELY sure your code is correct, you can use 'git commit --no-verify' to bypass this validation", 0);
38+
}
39+
40+
exit((int) $this->error);
41+
}
42+
43+
/**
44+
* @param string $output
45+
* @param int $level
46+
*/
47+
private function writeln($output = '', $level = 1)
48+
{
49+
$this->write($output, $level);
50+
echo PHP_EOL;
51+
}
52+
53+
/**
54+
* @param string $output
55+
* @param int $level
56+
*/
57+
private function write($output = '', $level = 1)
58+
{
59+
$spaces = $level * 3;
60+
61+
echo str_pad($output, strlen($output) + $spaces, ' ', STR_PAD_LEFT);
62+
}
63+
64+
/**
65+
* @return bool
66+
*/
67+
private function isRebase()
68+
{
69+
$output = [];
70+
exec('git symbolic-ref --short -q HEAD', $output);
71+
72+
return empty($output);
73+
}
74+
75+
/**
76+
* @param string $extension
77+
* @return string[]
78+
*/
79+
private function getCommittedFileList($extension = 'php')
80+
{
81+
exec("git diff --name-only --diff-filter=ACMRTUXB \"HEAD\" | grep -e '\." . $extension . "$'", $fileList);
82+
83+
return $fileList;
84+
}
85+
86+
/**
87+
* @param array $fileList
88+
*/
89+
private function runPhpLint(array $fileList)
90+
{
91+
$this->writeln('# Checking php syntax');
92+
$this->writeln('> php -l');
93+
94+
foreach ($fileList as $file) {
95+
exec('php -l ' . escapeshellarg($file) . ' 2> /dev/null', $output, $return);
96+
if ($return !== 0) {
97+
$this->writeln('- ' . $output[1], 2);
98+
$this->error = true;
99+
}
100+
}
101+
102+
$this->writeln();
103+
}
104+
105+
/**
106+
* @param array $fileList
107+
*/
108+
private function runPhpCsFixer(array $fileList)
109+
{
110+
$this->writeln('# Checking php code style');
111+
$this->writeln('> php-cs-fixer fix -v --no-ansi --dry-run');
112+
113+
if (!$this->isPHPCSFixerAvailable()) {
114+
$this->error = true;
115+
$this->writeln('- php-cs-fixer is NOT installed. Please install composer with dev dependencies.', 2);
116+
$this->writeln();
117+
118+
return;
119+
}
120+
121+
$fixes = [];
122+
foreach ($fileList as $file) {
123+
exec('./../../../../../../vendor/bin/php-cs-fixer fix -v --no-ansi --dry-run ' . escapeshellarg($file) . ' 2>&1', $output, $return);
124+
125+
if ($return !== 0) {
126+
$this->writeln('- ' . preg_replace('#^(\s+)?\d\)\s#', '', $output[3]), 2);
127+
$fixes[] = './../../../../../../vendor/bin/php-cs-fixer fix -v ' . escapeshellarg($file);
128+
$this->error = true;
129+
}
130+
}
131+
132+
if (!empty($fixes)) {
133+
$this->writeln();
134+
$this->writeln('Help:', 2);
135+
foreach ($fixes as $fix) {
136+
$this->writeln($fix, 3);
137+
}
138+
}
139+
140+
$this->writeln();
141+
}
142+
143+
/**
144+
* @param array $fileList
145+
*/
146+
private function runEsLint(array $fileList)
147+
{
148+
$this->writeln('# Checking javascript code style');
149+
$this->writeln('> eslint.js --ignore-path .eslintignore');
150+
151+
if (!$this->isESLintAvailable()) {
152+
$this->writeln('- eslint.js not found. Skipping javascript code style check.', 2);
153+
$this->writeln();
154+
155+
return;
156+
}
157+
158+
$this->checkESLint($fileList);
159+
160+
$this->writeln();
161+
}
162+
163+
/**
164+
* @return bool
165+
*/
166+
private function isPHPCSFixerAvailable()
167+
{
168+
$output = [];
169+
$return = 0;
170+
exec('command -v ./../../../../../../vendor/bin/php-cs-fixer >/dev/null 2>&1', $output, $return);
171+
172+
return !(bool) $return;
173+
}
174+
175+
/**
176+
* @return bool
177+
*/
178+
private function isESLintAvailable()
179+
{
180+
$output = [];
181+
$return = 0;
182+
exec('command -v ./../../../../../../themes/node_modules/eslint/bin/eslint.js >/dev/null 2>&1', $output, $return);
183+
184+
return !(bool) $return;
185+
}
186+
187+
/**
188+
* @param array $fileList
189+
*/
190+
private function checkESLint(array $fileList = [])
191+
{
192+
$output = [];
193+
$return = 0;
194+
exec(
195+
'./../../../../../../themes/node_modules/eslint/bin/eslint.js ' .
196+
'--ignore-path .eslintignore ' .
197+
'-c ./../../../../../../themes/.eslintrc.js ' .
198+
'--global "Ext, Shopware" ' .
199+
implode(' ', $fileList),
200+
$output,
201+
$return
202+
);
203+
$return = !(bool) $return;
204+
205+
if (!$return) {
206+
$this->error = true;
207+
208+
foreach ($output as $line) {
209+
$this->writeln($line, 2);
210+
}
211+
212+
$this->writeln('Help:', 2);
213+
$this->writeln(
214+
'./../../../../../../themes/node_modules/eslint/bin/eslint.js ' .
215+
'--fix --ignore-path .eslintignore ' .
216+
'-c ./../../../../../../themes/.eslintrc.js ' .
217+
'--global "Ext, Shopware" ' .
218+
implode(' ', $fileList),
219+
3
220+
);
221+
}
222+
}
223+
}
224+
225+
$checks = new PreCommitChecks();
226+
$checks->run();

.php_cs.dist

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__)
5+
;
6+
7+
$header = <<<EOF
8+
(c) shopware AG <[email protected]>
9+
10+
For the full copyright and license information, please view the LICENSE
11+
file that was distributed with this source code.
12+
EOF;
13+
14+
return PhpCsFixer\Config::create()
15+
->setUsingCache(false)
16+
->setRules([
17+
'@PSR2' => true,
18+
'@Symfony' => true,
19+
'header_comment' => ['header' => $header, 'separate' => 'bottom', 'commentType' => 'PHPDoc'],
20+
'no_useless_else' => true,
21+
'no_useless_return' => true,
22+
'ordered_class_elements' => true,
23+
'ordered_imports' => true,
24+
'phpdoc_order' => true,
25+
'phpdoc_summary' => false,
26+
'blank_line_after_opening_tag' => false,
27+
'concat_space' => ['spacing' => 'one'],
28+
'array_syntax' => ['syntax' => 'short']
29+
])
30+
->setFinder($finder)
31+
;

0 commit comments

Comments
 (0)