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

little-apps / LittleJWT / 13623116845

03 Mar 2025 03:59AM UTC coverage: 83.535%. Remained the same
13623116845

push

github

little-apps
Fixed styling

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1314 of 1573 relevant lines covered (83.53%)

424.99 hits per line

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

75.0
/src/Commands/Concerns/GeneratesEnvVariables.php
1
<?php
2

3
namespace LittleApps\LittleJWT\Commands\Concerns;
4

5
trait GeneratesEnvVariables
6
{
7
    /**
8
     * Updates the specified env file with the specified variables.
9
     *
10
     * @param  string  $envPath  Path to .env file
11
     * @param  array  $variables  Associative array of environment variables to append or replace.
12
     * @return bool True if .env file was updated.
13
     */
14
    protected function updateEnvFile(string $envPath, array $variables)
15
    {
16
        if (($contents = file_get_contents($envPath)) === false) {
30✔
17
            return false;
×
18
        }
19

20
        $contents = $this->updateEnvFileContents($contents, $variables);
30✔
21

22
        return file_put_contents($envPath, $contents) !== false;
30✔
23
    }
24

25
    /**
26
     * Updates the contents of the .env file.
27
     *
28
     * @param  string  $contents  Existing contents of .env file
29
     * @param  array  $variables  Associative array of environment variables to append or replace.
30
     * @return string Updated .env file contents
31
     */
32
    protected function updateEnvFileContents(string $contents, array $variables)
33
    {
34
        foreach ($variables as $key => $value) {
30✔
35
            if (! $this->envKeyExists($key)) {
30✔
36
                // Append to .env file
UNCOV
37
                $contents = $this->appendEnvFile($contents, $key, $value);
3✔
38
            } else {
39
                // Replace in .env file
40
                $contents = $this->replaceEnvFile($contents, $key, $value);
27✔
41
            }
42
        }
43

44
        return $contents;
30✔
45
    }
46

47
    /**
48
     * Checks if env key already exists.
49
     *
50
     * @return bool
51
     */
52
    protected function envKeyExists(string $key)
53
    {
54
        return ! is_null(env($key));
30✔
55
    }
56

57
    /**
58
     * Appends env variable to file contents.
59
     *
60
     * @param  string  $contents  Existing .env file contents
61
     * @param  string  $key  Environment key
62
     * @param  mixed  $value  Value
63
     * @return string Updated contents
64
     */
65
    protected function appendEnvFile(string $contents, string $key, $value)
66
    {
UNCOV
67
        return sprintf('%s%s%s%s', $contents, PHP_EOL, $this->createLineForEnvFile($key, $value), PHP_EOL);
3✔
68
    }
69

70
    /**
71
     * Replaces existing env variable in file.
72
     *
73
     * @param  string  $contents  Existing .env file contents
74
     * @param  string  $key  Environment key
75
     * @param  mixed  $value  Value
76
     * @return string Updated contents
77
     */
78
    protected function replaceEnvFile(string $contents, string $key, $value)
79
    {
80
        $regex = sprintf('/^(%s)=([^\r\n]+)$/m', $key);
27✔
81
        $sub = $this->createLineForEnvFile($key, $value);
27✔
82

83
        return preg_replace($regex, $sub, $contents);
27✔
84
    }
85

86
    /**
87
     * Generates line to insert into .env file.
88
     *
89
     * @param  string  $key  Key
90
     * @param  string  $value  Value
91
     * @return string
92
     */
93
    protected function createLineForEnvFile(string $key, $value)
94
    {
95
        return sprintf('%s=%s', $key, $this->transformEnvValue($value));
60✔
96
    }
97

98
    /**
99
     * Transforms value so it can be stored in .env file.
100
     *
101
     * @param  mixed  $value
102
     * @return string
103
     */
104
    protected function transformEnvValue($value)
105
    {
106
        switch (gettype($value)) {
60✔
107
            case 'boolean':
60✔
108
                $value = $value ? 'true' : 'false';
×
109

110
                break;
×
111
            case 'NULL':
60✔
112
                $value = 'null';
×
113

114
                break;
×
115
            case 'string':
60✔
116
                $value = sprintf('"%s"', $value);
60✔
117

118
                break;
60✔
119

120
            default:
121
                $value = (string) $value;
×
122

123
                break;
×
124
        }
125

126
        return $value;
60✔
127
    }
128

129
    /**
130
     * Gets the .env file path.
131
     *
132
     * @return string
133
     */
134
    protected function envPath()
135
    {
136
        if (method_exists($this->laravel, 'environmentFilePath')) {
30✔
137
            return $this->laravel->environmentFilePath();
30✔
138
        } else {
139
            return sprintf('%s%s%s', $this->laravel->basePath(), DIRECTORY_SEPARATOR, '.env');
×
140
        }
141
    }
142

143
    /**
144
     * Checks if key is valid for .env file.
145
     */
146
    protected function isEnvKeyValid(string $key): bool
147
    {
148
        return (bool) preg_match('/^[A-Za-z0-9_]+$/', $key);
70✔
149
    }
150
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc