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

The-oGlow / ezkoding / 32839533896

25 Aug 2026 10:53AM UTC coverage: 86.082% (+0.8%) from 85.249%
32839533896

push

github

ollily
Merge commit '55d80e110'

232 of 270 new or added lines in 12 files covered. (85.93%)

8 existing lines in 3 files now uncovered.

569 of 661 relevant lines covered (86.08%)

7.55 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

23
    public const string ENV_USERPROFILE = 'USERPROFILE';
24

25
    public const int ENV_OFFSET = 2;
26

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

47
        return $home;
4✔
48
    }
49

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

63
        return (string)realpath($projectRoot);
1✔
64
    }
65

66
    /**
67
     * @param string $subFolder A folder below the temp folder (optional)
68
     *
69
     * @return string The system wide folder for temporarily files
70
     */
71
    final public static function getSystemTemp(string $subFolder = ''): string
7✔
72
    {
73
        $tmp = sys_get_temp_dir();
7✔
74
        if (!empty($subFolder)) {
7✔
75
            $tmp .= DIRECTORY_SEPARATOR . $subFolder;
6✔
76
        }
77

78
        return $tmp;
7✔
79
    }
80

81
    /**
82
     * @return string The full path to this composer project
83
     */
84
    private static function getComposerFilePath(): string
2✔
85
    {
86
        $composerFile = \Composer\Factory::getComposerFile();
2✔
87
        $composerPath = (string)realpath(dirname($composerFile));
2✔
88
        if ('.' == $composerPath) {
2✔
89
            $composerPath = '';
×
90
        }
91

92
        return $composerPath;
2✔
93
    }
94

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

105
        return (string)realpath($rootPath . str_repeat(DIRECTORY_SEPARATOR . '..', $folderOffset));
1✔
106
    }
107
}
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