Completed
Push — master ( 63d775...2b54e4 )
by Sebastian
02:42
created

Template::getTplTargetPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
crap 6
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace sebastianfeldmann\CaptainHook\Hook;
11
12
use sebastianfeldmann\CaptainHook\Storage\Util;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, sebastianfeldmann\CaptainHook\Hook\Util.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * Template class
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
abstract class Template
23
{
24
    /**
25
     * Return the php code for the git hook scripts.
26
     *
27
     * @param  string $hook       Name of the hook to trigger.
28 3
     * @param  string $repoPath   Absolute path to the repository root.
29
     * @param  string $vendorPath Absolute path to the vendor folder.
30 3
     * @param  string $configPath Absolute path to the configuration file.
31 3
     * @return string
32 3
     */
33 3
    public static function getCode($hook, $repoPath, $vendorPath, $configPath)
34 3
    {
35 3
        $tplVendorPath = self::getTplTargetPath($repoPath, $vendorPath);
36 3
        $tplConfigPath = self::getTplTargetPath($repoPath, $configPath);
37 3
38 3
        return '#!/usr/bin/env php' . PHP_EOL .
39 3
               '<?php' . PHP_EOL .
40 3
               '$autoLoader = ' . $tplVendorPath . '/autoload.php\';' . PHP_EOL . PHP_EOL .
41 3
               'if (!file_exists($autoLoader)) {' . PHP_EOL .
42 3
               '    fwrite(STDERR,' . PHP_EOL .
43 3
               '        \'Composer autoload.php could not be found\' . PHP_EOL .' . PHP_EOL .
44 3
               '        \'Please re-install the hook with:\' . PHP_EOL .' . PHP_EOL .
45 3
               '        \'$ captainhook install --composer-vendor-path=...\' . PHP_EOL' . PHP_EOL .
46 3
               '    );' . PHP_EOL .
47
               '    exit(1);' . PHP_EOL .
48
               '}' . PHP_EOL .
49
               'require $autoLoader;' . PHP_EOL .
50
               '$config = realpath(' . $tplConfigPath . '\');' . PHP_EOL .
51
               '$app    = new sebastianfeldmann\CaptainHook\Console\Application\Hook();' . PHP_EOL .
52
               '$app->setHook(\'' . $hook . '\');' . PHP_EOL .
53
               '$app->setConfigFile($config);' . PHP_EOL .
54
               '$app->run();' . PHP_EOL . PHP_EOL;
55
    }
56
57
    /**
58
     * Return the path to the target path from inside the .git/hooks directory f.e. __DIR__ ../../vendor.
59
     *
60
     * @param  string $repoDir
61
     * @param  string $targetPath
62
     * @return string
63
     * @throws \RuntimeException
64
     */
65
    public static function getTplTargetPath($repoDir, $targetPath)
66
    {
67
        $repo    = explode(DIRECTORY_SEPARATOR, ltrim($repoDir, DIRECTORY_SEPARATOR));
68
        $target  = explode(DIRECTORY_SEPARATOR, ltrim($targetPath, DIRECTORY_SEPARATOR));
69
70
        if (!Util::isSubDirectoryOf($target, $repo)) {
71
            return '\'' . $targetPath;
72
        }
73
74
        return '__DIR__ . \'/../../' . Util::getSubPathOf($target, $repo);
75
    }
76
}
77