• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

The-oGlow / ezkoding / 28261992495

26 Jun 2026 07:57PM UTC coverage: 79.436% (+0.3%) from 79.125%
28261992495

push

github

ollily
#2: add twig templating

14 of 15 new or added lines in 2 files covered. (93.33%)

479 of 603 relevant lines covered (79.44%)

6.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/src/Tools/EnvironmentVariableTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of ezkoding
7
 *
8
 * (c) 2025 Oliver Glowa, coding.glowa.com
9
 *
10
 * This source file is subject to the Apache-2.0 license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace ollily\Tools;
15

16
/**
17
 * Provide some common used environment variables.
18
 */
19
trait EnvironmentVariableTrait
20
{
21
    public const string ENV_HOME='HOME';
22
    public const string ENV_USERPROFILE = 'USERPROFILE';
23
    public const int ENV_OFFSET = 2;
24

25
    /**
26
     * @param string $homeVariable Ignore parameter to use the standard settings
27
     * 
28
     * @return string Full path to the home directory of the current user
29
     * 
30
     * @see {@link EnvironmentVariableTrait::ENV_HOME}
31
     * @see {@link EnvironmentVariableTrait::ENV_USERPROFILE}
32
     */
33
    final public static function getHome(string $homeVariable = self::ENV_HOME): string
3✔
34
    {
35
        $home = getenv($homeVariable);
3✔
36
        /** @psalm-suppress RiskyTruthyFalsyComparison */
37
        if ($homeVariable == self::ENV_HOME && empty($home)) {
3✔
38
            // we are on windows?
NEW
39
            $home = getenv(self::ENV_USERPROFILE);
×
40
        }
41
        if (is_bool($home)) {
3✔
42
            $home = '';
2✔
43
        }
44

45
        return $home;
3✔
46
    }
47

48
    /**
49
     * @return string The root folder of this php project
50
     * 
51
     * @see {@link EnvironmentVariableTrait::getComposerFilePath()}
52
     * @see {@link EnvironmentVariableTrait::getProjectRootFallback()}
53
     */
54
    final public static function getProjectRoot(): string
1✔
55
    {
56
        $projectRoot = self::getComposerFilePath();
1✔
57
        if (empty($projectRoot)) {
1✔
58
            $projectRoot = self::getProjectRootFallback();
×
59
        }
60

61
        return (string)realpath($projectRoot);
1✔
62
    }
63

64
    /**
65
     * @param string $subFolder A folder below the temp folder (optional)
66
     * 
67
     * @return string The system wide folder for temporarily files
68
     */
69
    final public static function getSystemTemp(string $subFolder=''): string {
5✔
70
        $tmp = sys_get_temp_dir();
5✔
71
        if (!empty($subFolder)) {
5✔
72
            $tmp .= DIRECTORY_SEPARATOR . $subFolder;
4✔
73
        }
74
        return $tmp;
5✔
75
    }
76
    
77
    /**
78
     * @return string The full path to this composer project
79
     */
80
    private static function getComposerFilePath(): string
2✔
81
    {
82
        $composerFile = \Composer\Factory::getComposerFile();
2✔
83
        $composerPath = (string)realpath(dirname($composerFile));
2✔
84
        if ('.' == $composerPath) {
2✔
85
            $composerPath = '';
×
86
        }
87

88
        return $composerPath;
2✔
89
    }
90

91
    /**
92
     * @param int $folderOffset Ignore the parameter to use the default
93
     * 
94
     * @return string Calculate the root folder of the project
95
     */
96
    private static function getProjectRootFallback(int $folderOffset = self::ENV_OFFSET): string
1✔
97
    {
98
        $rootClazz = new \ReflectionClass(EnvironmentVariableTrait::class);
1✔
99
        $rootPath  = dirname((string)realpath((string)$rootClazz->getFileName()));
1✔
100

101
        return (string)realpath($rootPath . str_repeat(DIRECTORY_SEPARATOR . '..', $folderOffset));
1✔
102
    }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc