Skip to content

Commit d3ba750

Browse files
joaorobertopbBrainMaestro
authored andcommitted
Code Improvements (#23)
* Add 'windows_os' function - DRY ( Don't repeat yourself concept ) * Code cleanup * Add 'mkdir_if_not_exist' function - DRY ( Don't repeat yourself concept ) * Add 'git_version' function - Avoid updating the application version in future releases * Sort functions * Rename functions - 'windos_os()' to 'is_windows()' - 'mkdir_if_not_exist()' to 'create_hooks_dir()' - Replace mkdir hardcode with 'create_hooks_dir()' function * Update 'create_hooks_dir()' function - String interpolation within function
1 parent 283dd5b commit d3ba750

12 files changed

+69
-26
lines changed

cghooks

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use BrainMaestro\GitHooks\Commands\ListCommand;
2222
use BrainMaestro\GitHooks\Commands\HookCommand;
2323
use Symfony\Component\Console\Application;
2424

25-
$application = new Application('Composer Git Hooks', 'v2.2.0');
25+
$application = new Application('Composer Git Hooks', git_version());
2626

2727
$hooks = Hook::getValidHooks($dir);
2828
$application->add(new AddCommand($hooks));

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"autoload": {
2727
"psr-4": {
2828
"BrainMaestro\\GitHooks\\": "src/"
29-
}
29+
},
30+
"files": [
31+
"src/helpers.php"
32+
]
3033
},
3134
"autoload-dev": {
3235
"psr-4": {

src/Commands/AddCommand.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
3838
$gitDir = $input->getOption('git-dir');
3939
$force = $input->getOption('force');
4040
$forceWindows = $input->getOption('force-win');
41-
$hookDir = "{$gitDir}/hooks";
4241

43-
if (! is_dir($hookDir)) {
44-
mkdir($hookDir, 0700, true);
45-
}
42+
create_hooks_dir($gitDir);
4643

4744
foreach ($this->hooks as $hook => $script) {
4845
$filename = "{$gitDir}/hooks/{$hook}";
@@ -51,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5148
if (! $force && $fileExists) {
5249
$output->writeln("<comment>{$hook} already exists</comment>");
5350
} else {
54-
if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
51+
if ($forceWindows || is_windows()) {
5552
// On windows we need to add a SHEBANG
5653
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
5754
$script = '#!/bin/bash' . PHP_EOL . $script;

src/Commands/RemoveCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
6060
unlink($filename);
6161
$output->writeln("Removed <info>{$hook}</info> hook");
6262
unset($lockFileHooks[$hook]);
63-
} else {
64-
$output->writeln("<error>{$hook} hook does not exist</error>");
63+
continue;
6564
}
65+
66+
$output->writeln("<error>{$hook} hook does not exist</error>");
6667
}
6768

6869
file_put_contents(Hook::LOCK_FILE, json_encode(array_keys($lockFileHooks)));

src/Commands/UpdateCommand.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
3333
{
3434
$gitDir = $input->getOption('git-dir');
3535
$forceWindows = $input->getOption('force-win');
36-
$hookDir = "{$gitDir}/hooks";
37-
38-
if (! is_dir($hookDir)) {
39-
mkdir($hookDir, 0700, true);
40-
}
36+
37+
create_hooks_dir($gitDir);
4138

4239
foreach ($this->hooks as $hook => $script) {
4340
$filename = "{$gitDir}/hooks/{$hook}";
4441

4542
$operation = file_exists($filename) ? 'Updated' : 'Added';
4643

47-
if ($forceWindows || strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
44+
if ($forceWindows || is_windows()) {
4845
// On windows we need to add a SHEBANG
4946
// See: https://github.com/BrainMaestro/composer-git-hooks/issues/7
5047
$script = '#!/bin/bash' . PHP_EOL . $script;

src/Hook.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function getValidHooks($dir)
2525
$validHooks = [];
2626

2727
foreach ($hooks as $hook => $script) {
28-
if (array_key_exists($hook, self::getHooks())) {
28+
if (self::isValidHook($hook)) {
2929
$validHooks[$hook] = $script;
3030
}
3131
}

src/helpers.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
use Symfony\Component\Process\Process;
4+
5+
if (! function_exists('create_hooks_dir')) {
6+
/**
7+
* Create hook directory if not exists.
8+
*
9+
* @param string $dir
10+
* @param int $mode
11+
* @param bool $recursive
12+
*
13+
* @return void
14+
*/
15+
function create_hooks_dir($dir, $mode = 0700, $recursive = true)
16+
{
17+
if (! is_dir("{$dir}/hooks")) {
18+
mkdir("{$dir}/hooks", $mode, $recursive);
19+
}
20+
}
21+
}
22+
23+
if (! function_exists('git_version')) {
24+
/**
25+
* Get latest git tag version.
26+
*
27+
* @return string
28+
*/
29+
function git_version()
30+
{
31+
$process = new Process('git describe --tags $(git rev-list --tags --max-count=1)');
32+
$process->run();
33+
34+
return trim($process->getOutput()) ?: 'unreleased';
35+
}
36+
}
37+
38+
if (! function_exists('is_windows')) {
39+
/**
40+
* Determine whether the current environment is Windows based.
41+
*
42+
* @return bool
43+
*/
44+
function is_windows()
45+
{
46+
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
47+
}
48+
}

tests/AddCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function it_adds_hooks_that_do_not_already_exist()
3838
*/
3939
public function it_adds_shebang_to_hooks_on_windows()
4040
{
41-
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
41+
if (! is_windows()) {
4242
$this->markTestSkipped('This test is only relevant on windows. You\'re running Linux.');
4343
}
4444

@@ -130,7 +130,7 @@ public function it_ignores_the_hook_lock_file_if_the_ignore_lock_option_is_passe
130130
public function it_uses_a_different_git_path_if_specified()
131131
{
132132
$gitDir = 'test-git-dir';
133-
mkdir("{$gitDir}/hooks", 0700, true);
133+
create_hooks_dir($gitDir);
134134
$this->commandTester->execute(['--git-dir' => $gitDir]);
135135

136136
foreach (array_keys(self::$hooks) as $hook) {

tests/ListCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function it_uses_a_different_git_path_if_specified()
3939
{
4040
$gitDir = 'test-git-dir';
4141

42-
mkdir("{$gitDir}/hooks", 0777, true);
42+
create_hooks_dir($gitDir, 0777);
4343

4444
self::createHooks($gitDir);
4545

tests/PrepareHookTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public static function tearDownAfterClass()
2323

2424
public static function createHooks($gitDir = '.git')
2525
{
26-
if (!is_dir("{$gitDir}/hooks")) {
27-
mkdir("{$gitDir}/hooks", 0777, true);
28-
}
26+
create_hooks_dir($gitDir, 0777);
2927

3028
foreach (self::$hooks as $hook => $script) {
3129
file_put_contents("{$gitDir}/hooks/{$hook}", $script);

tests/RemoveCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function it_uses_a_different_git_path_if_specified()
9797
{
9898
$gitDir = 'test-git-dir';
9999

100-
mkdir("{$gitDir}/hooks", 0777, true);
100+
create_hooks_dir($gitDir, 0777);
101101

102102
self::createHooks($gitDir);
103103
$this->assertFalse(self::isDirEmpty("{$gitDir}/hooks"));

tests/UpdateCommandTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function it_updates_hooks_that_already_exist()
4848

4949
public function it_adds_shebang_to_hooks_on_windows()
5050
{
51-
if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
51+
if (! is_windows()) {
5252
$this->markTestSkipped('This test is only relevant on windows. You\'re running Linux.');
5353
}
5454

@@ -71,7 +71,7 @@ public function it_uses_a_different_git_path_if_specified()
7171
{
7272
$gitDir = 'test-git-dir';
7373

74-
mkdir("{$gitDir}/hooks", 0777, true);
74+
create_hooks_dir($gitDir, 0777);
7575

7676
$this->commandTester->execute(['--git-dir' => $gitDir]);
7777

@@ -104,7 +104,6 @@ public function it_create_git_hooks_path_when_hooks_dir_not_exists()
104104
rmdir($hookDir);
105105
}
106106

107-
108107
/**
109108
* @test
110109
*/

0 commit comments

Comments
 (0)