Skip to content

Commit 0af8c5e

Browse files
committed
feat: Adds methods for access, type conversions (string, integer, boolean), and validation.
1 parent 49c6af5 commit 0af8c5e

17 files changed

+806
-2
lines changed

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/tests export-ignore
2+
/vendor export-ignore
3+
4+
/LICENSE export-ignore
5+
/Makefile export-ignore
6+
/README.md export-ignore
7+
/phpmd.xml export-ignore
8+
/phpunit.xml export-ignore
9+
/phpstan.neon.dist export-ignore
10+
/infection.json.dist export-ignore
11+
12+
/.github export-ignore
13+
/.gitignore export-ignore
14+
/.gitattributes export-ignore

.github/workflows/auto-assign.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Auto assign issues
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
8+
jobs:
9+
run:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
issues: write
13+
pull-requests: write
14+
steps:
15+
- name: Assign issues
16+
uses: gustavofreze/auto-assign@1.0.0
17+
with:
18+
assignees: '${{ secrets.ASSIGNEES }}'
19+
github_token: '${{ secrets.GITHUB_TOKEN }}'
20+
allow_self_assign: 'true'
21+
allow_no_assignees: 'true'
22+
assignment_options: 'ISSUE'

.github/workflows/ci.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
permissions:
8+
contents: read
9+
10+
env:
11+
PHP_VERSION: '8.3'
12+
13+
jobs:
14+
auto-review:
15+
name: Auto review
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@v4
21+
22+
- name: Configure PHP
23+
uses: shivammathur/setup-php@v2
24+
with:
25+
php-version: ${{ env.PHP_VERSION }}
26+
27+
- name: Install dependencies
28+
run: composer update --no-progress --optimize-autoloader
29+
30+
- name: Run review
31+
run: composer review
32+
33+
tests:
34+
name: Tests
35+
runs-on: ubuntu-latest
36+
37+
steps:
38+
- name: Checkout
39+
uses: actions/checkout@v4
40+
41+
- name: Use PHP ${{ env.PHP_VERSION }}
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ env.PHP_VERSION }}
45+
46+
- name: Install dependencies
47+
run: composer update --no-progress --optimize-autoloader
48+
49+
- name: Run tests
50+
run: composer tests

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
3+
/vendor/
4+
/report
5+
*.lock
6+
.phpunit.*

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.3
2+
3+
.PHONY: configure test test-file test-no-coverage review show-reports clean
4+
5+
configure:
6+
@${DOCKER_RUN} composer update --optimize-autoloader
7+
8+
test:
9+
@${DOCKER_RUN} composer tests
10+
11+
test-file:
12+
@${DOCKER_RUN} composer tests-file-no-coverage ${FILE}
13+
14+
test-no-coverage:
15+
@${DOCKER_RUN} composer tests-no-coverage
16+
17+
review:
18+
@${DOCKER_RUN} composer review
19+
20+
show-reports:
21+
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html
22+
23+
clean:
24+
@sudo chown -R ${USER}:${USER} ${PWD}
25+
@rm -rf report vendor .phpunit.cache .lock

README.md

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,89 @@
1-
# environment-variable
2-
Provides a simple and flexible solution for managing environment variables, with easy access, type conversions, and validation handling.
1+
# Environment variable
2+
3+
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
4+
5+
* [Overview](#overview)
6+
* [Installation](#installation)
7+
* [How to use](#how-to-use)
8+
* [License](#license)
9+
* [Contributing](#contributing)
10+
11+
<div id='overview'></div>
12+
13+
## Overview
14+
15+
Provides a simple and flexible solution for managing environment variables, with easy access, type conversions, and
16+
validation handling.
17+
18+
<div id='installation'></div>
19+
20+
## Installation
21+
22+
```bash
23+
composer require tiny-blocks/environment-variable
24+
```
25+
26+
<div id='how-to-use'></div>
27+
28+
## How to use
29+
30+
### Creating an environment variable
31+
32+
To create and work with environment variables, use the `from` method to get an instance of the environment variable.
33+
34+
```php
35+
EnvironmentVariable::from(name: 'MY_VAR');
36+
```
37+
38+
### Conversions
39+
40+
Once you have an instance of the environment variable, you can convert its value into various types.
41+
42+
#### Convert to string
43+
44+
To convert the environment variable to a string.
45+
46+
```php
47+
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
48+
$environmentVariable->toString();
49+
```
50+
51+
#### Convert to integer
52+
53+
To convert the environment variable to an integer.
54+
55+
```php
56+
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
57+
$environmentVariable->toInteger();
58+
```
59+
60+
#### Convert to boolean
61+
62+
To convert the environment variable to a boolean.
63+
64+
```php
65+
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
66+
$environmentVariable->toBoolean();
67+
```
68+
69+
### Check if the environment variable has a value
70+
71+
Checks if the environment variable has a value. Values like `false`, `0`, and `-1` are valid and non-empty.
72+
73+
```php
74+
$environmentVariable = EnvironmentVariable::from(name: 'MY_VAR');
75+
$environmentVariable->hasValue();
76+
```
77+
78+
<div id='license'></div>
79+
80+
## License
81+
82+
Environment variable is licensed under [MIT](LICENSE).
83+
84+
<div id='contributing'></div>
85+
86+
## Contributing
87+
88+
Please follow the [contributing guidelines](https://github.com/tiny-blocks/tiny-blocks/blob/main/CONTRIBUTING.md) to
89+
contribute to the project.

composer.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{
2+
"name": "tiny-blocks/environment-variable",
3+
"type": "library",
4+
"license": "MIT",
5+
"homepage": "https://github.com/tiny-blocks/environment-variable",
6+
"description": "Provides a simple and flexible solution for managing environment variables, with easy access, type conversions, and validation handling.",
7+
"prefer-stable": true,
8+
"minimum-stability": "stable",
9+
"keywords": [
10+
"psr",
11+
"env",
12+
"tiny-blocks",
13+
"environment-variable"
14+
],
15+
"authors": [
16+
{
17+
"name": "Gustavo Freze de Araujo Santos",
18+
"homepage": "https://github.com/gustavofreze"
19+
}
20+
],
21+
"support": {
22+
"issues": "https://github.com/tiny-blocks/environment-variable/issues",
23+
"source": "https://github.com/tiny-blocks/environment-variable"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"TinyBlocks\\EnvironmentVariable\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"TinyBlocks\\EnvironmentVariable\\": "tests/"
33+
}
34+
},
35+
"require": {
36+
"php": "^8.3"
37+
},
38+
"require-dev": {
39+
"phpmd/phpmd": "^2.15",
40+
"phpunit/phpunit": "^11",
41+
"phpstan/phpstan": "^1",
42+
"dg/bypass-finals": "^1.8",
43+
"infection/infection": "^0",
44+
"squizlabs/php_codesniffer": "^3"
45+
},
46+
"config": {
47+
"sort-packages": true,
48+
"allow-plugins": {
49+
"infection/extension-installer": true
50+
}
51+
},
52+
"scripts": {
53+
"test": "phpunit --configuration phpunit.xml tests",
54+
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
55+
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --ignore-violations-on-exit",
56+
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
57+
"mutation-test": "infection --only-covered --threads=max --logger-html=report/coverage/mutation-report.html --coverage=report/coverage",
58+
"test-no-coverage": "phpunit --configuration phpunit.xml --no-coverage tests",
59+
"review": [
60+
"@phpcs",
61+
"@phpmd",
62+
"@phpstan"
63+
],
64+
"tests": [
65+
"@test",
66+
"@mutation-test"
67+
],
68+
"tests-no-coverage": [
69+
"@test-no-coverage"
70+
]
71+
}
72+
}

infection.json.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"logs": {
3+
"text": "report/infection/logs/infection-text.log",
4+
"summary": "report/infection/logs/infection-summary.log"
5+
},
6+
"tmpDir": "report/infection/",
7+
"minMsi": 100,
8+
"timeout": 30,
9+
"source": {
10+
"directories": [
11+
"src"
12+
]
13+
},
14+
"phpUnit": {
15+
"configDir": "",
16+
"customPath": "./vendor/bin/phpunit"
17+
},
18+
"mutators": {
19+
"@default": true
20+
},
21+
"minCoveredMsi": 100,
22+
"testFramework": "phpunit"
23+
}

phpmd.xml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHPMD Custom rules"
3+
xmlns="http://pmd.sf.net/ruleset/1.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
6+
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
7+
<description>PHPMD Custom rules</description>
8+
9+
<rule ref="rulesets/cleancode.xml/ElseExpression"/>
10+
<rule ref="rulesets/cleancode.xml/BooleanArgumentFlag"/>
11+
12+
<rule ref="rulesets/codesize.xml/TooManyFields"/>
13+
<rule ref="rulesets/codesize.xml/TooManyMethods"/>
14+
<rule ref="rulesets/codesize.xml/NPathComplexity"/>
15+
<rule ref="rulesets/codesize.xml/CyclomaticComplexity"/>
16+
<rule ref="rulesets/codesize.xml/ExcessivePublicCount"/>
17+
<rule ref="rulesets/codesize.xml/ExcessiveClassLength"/>
18+
<rule ref="rulesets/codesize.xml/ExcessiveMethodLength"/>
19+
<rule ref="rulesets/codesize.xml/ExcessiveParameterList"/>
20+
<rule ref="rulesets/codesize.xml/ExcessiveClassComplexity"/>
21+
22+
<rule ref="rulesets/controversial.xml/Superglobals"/>
23+
<rule ref="rulesets/controversial.xml/CamelCaseClassName"/>
24+
<rule ref="rulesets/controversial.xml/CamelCaseMethodName"/>
25+
<rule ref="rulesets/controversial.xml/CamelCasePropertyName"/>
26+
<rule ref="rulesets/controversial.xml/CamelCaseVariableName"/>
27+
<rule ref="rulesets/controversial.xml/CamelCaseParameterName"/>
28+
29+
<rule ref="rulesets/design.xml/GotoStatement"/>
30+
<rule ref="rulesets/design.xml/ExitExpression"/>
31+
<rule ref="rulesets/design.xml/EvalExpression"/>
32+
<rule ref="rulesets/design.xml/NumberOfChildren"/>
33+
<rule ref="rulesets/design.xml/DepthOfInheritance"/>
34+
<rule ref="rulesets/design.xml/DevelopmentCodeFragment"/>
35+
36+
<rule ref="rulesets/naming.xml/LongVariable">
37+
<properties>
38+
<property name="maximum" value="25"/>
39+
</properties>
40+
</rule>
41+
<rule ref="rulesets/naming.xml/ShortVariable">
42+
<properties>
43+
<property name="minimum" value="2"/>
44+
</properties>
45+
</rule>
46+
<rule ref="rulesets/naming.xml/ShortMethodName">
47+
<properties>
48+
<property name="minimum" value="2"/>
49+
</properties>
50+
</rule>
51+
<rule ref="rulesets/naming.xml/BooleanGetMethodName"/>
52+
<rule ref="rulesets/naming.xml/ConstantNamingConventions"/>
53+
<rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>
54+
55+
<rule ref="rulesets/unusedcode.xml/UnusedPrivateField"/>
56+
<rule ref="rulesets/unusedcode.xml/UnusedLocalVariable"/>
57+
<rule ref="rulesets/unusedcode.xml/UnusedPrivateMethod"/>
58+
<rule ref="rulesets/unusedcode.xml/UnusedFormalParameter"/>
59+
</ruleset>

0 commit comments

Comments
 (0)