Skip to content

Commit b4820a0

Browse files
authored
refactor: Improves strict typing and tests. Also updates documentation for Alpha codes and Country usage examples. (#10)
1 parent c23ac8f commit b4820a0

22 files changed

+349
-146
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+
/README.md export-ignore
5+
/LICENSE export-ignore
6+
/Makefile 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/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ jobs:
1616
- name: Checkout
1717
uses: actions/checkout@v3
1818

19+
- name: Use PHP 8.2
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: '8.2'
23+
1924
- name: Install dependencies
2025
run: composer update --no-progress --optimize-autoloader
2126

@@ -33,6 +38,11 @@ jobs:
3338
- name: Checkout
3439
uses: actions/checkout@v3
3540

41+
- name: Use PHP 8.2
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: '8.2'
45+
3646
- name: Install dependencies
3747
run: composer update --no-progress --optimize-autoloader
3848

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2
22

3-
.PHONY: configure test test-no-coverage review show-reports clean
3+
.PHONY: configure test test-file test-no-coverage review fix-style show-reports clean
44

55
configure:
66
@${DOCKER_RUN} composer update --optimize-autoloader
77

8-
test: review
8+
test:
99
@${DOCKER_RUN} composer tests
1010

11-
test-no-coverage: review
11+
test-file:
12+
@${DOCKER_RUN} composer tests-file-no-coverage ${FILE}
13+
14+
test-no-coverage:
1215
@${DOCKER_RUN} composer tests-no-coverage
1316

1417
review:
1518
@${DOCKER_RUN} composer review
1619

20+
fix-style:
21+
@${DOCKER_RUN} composer fix-style
22+
1723
show-reports:
1824
@sensible-browser report/coverage/coverage-html/index.html report/coverage/mutation-report.html
1925

README.md

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
## Overview
1414

15-
Value Object representing a country using ISO-3166 specifications.
15+
Value Object representing a country using [ISO-3166 specifications](https://www.iso.org/iso-3166-country-codes.html).
1616

1717
<div id='installation'></div>
1818

@@ -26,12 +26,13 @@ composer require tiny-blocks/country
2626

2727
## How to use
2828

29-
The library exposes country codes according to ISO-3166 specifications. Also, it is possible to create a
29+
The library exposes country codes according
30+
to [ISO-3166 specifications](https://www.iso.org/iso-3166-country-codes.html). Also, it is possible to create a
3031
representation of a country that groups the codes and its name.
3132

3233
### Alpha2Code
3334

34-
**Alpha-2 code**: a two-letter code that represents a country name, recommended as the general purpose code.
35+
A two-letter code that represents a country name, recommended as the general purpose code.
3536

3637
```php
3738
$alpha2Code = Alpha2Code::UNITED_STATES_OF_AMERICA;
@@ -43,7 +44,7 @@ $alpha2Code->toAlpha3()->value; # USA
4344

4445
### Alpha3Code
4546

46-
**Alpha-3 code**: a three-letter code that represents a country name, which is usually more closely related to the
47+
A three-letter code that represents a country name, which is usually more closely related to the
4748
country name.
4849

4950
```php
@@ -56,11 +57,15 @@ $alpha3Code->toAlpha2()->value; # US
5657

5758
### Country
5859

59-
A country might change a significant part of its name, so there is no specific ISO for this case.
60+
A `Country` instance can be created using either an `Alpha-2` or `Alpha-3` code, along with an optional country name.
61+
There are two main methods to create a `Country` object: `from` (which accepts objects) and `fromString` (which accepts
62+
strings).
6063

61-
You can create an instance of `Country`, using the `from` method, informing an alpha code (`Alpha2Code`or `Alpha3Code`),
62-
where they can be of type `AlphaCode` or just `strings`. Optionally, you can enter the name of the country. If the
63-
country name is not given in the `from` method, then the default is to assume an English version of the country name.
64+
#### Creating from objects
65+
66+
You can create a `Country` instance using the `from` method by providing an `Alpha2Code` or `Alpha3Code` object.
67+
Optionally, you can pass the name of the country. If no name is provided, the default is the English version of the
68+
country name.
6469

6570
```php
6671
$country = Country::from(alphaCode: Alpha2Code::UNITED_STATES_OF_AMERICA);
@@ -73,14 +78,14 @@ $country->alpha3->value; # USA
7378
or
7479

7580
```php
76-
$country = Country::from(alphaCode: 'US');
81+
$country = Country::from(alphaCode: Alpha3Code::UNITED_STATES_OF_AMERICA);
7782

7883
$country->name; # United States of America
7984
$country->alpha2->value; # US
8085
$country->alpha3->value; # USA
8186
```
8287

83-
Creating an instance passing the country name.
88+
If you want to specify a custom name:
8489

8590
```php
8691
$country = Country::from(alphaCode: Alpha3Code::UNITED_STATES_OF_AMERICA, name: 'United States');
@@ -90,6 +95,29 @@ $country->alpha2->value; # US
9095
$country->alpha3->value; # USA
9196
```
9297

98+
#### Creating from string
99+
100+
Alternatively, you can create a `Country` instance using the `fromString` method, which accepts an `Alpha-2` or
101+
`Alpha-3` code as a string. This method is useful when the alpha code is provided as a string.
102+
103+
```php
104+
$country = Country::fromString(alphaCode: 'US');
105+
106+
$country->name; # United States of America
107+
$country->alpha2->value; # US
108+
$country->alpha3->value; # USA
109+
```
110+
111+
You can also pass a custom country name when using the `fromString` method:
112+
113+
```php
114+
$country = Country::fromString(alphaCode: 'USA', name: 'United States');
115+
116+
$country->name; # United States
117+
$country->alpha2->value; # US
118+
$country->alpha3->value; # USA
119+
```
120+
93121
<div id='license'></div>
94122

95123
## License

composer.json

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
"keywords": [
1010
"vo",
1111
"psr",
12-
"psr-4",
13-
"psr-12",
1412
"country",
1513
"tiny-blocks",
1614
"value-object"
@@ -21,6 +19,10 @@
2119
"homepage": "https://github.com/gustavofreze"
2220
}
2321
],
22+
"support": {
23+
"issues": "https://github.com/tiny-blocks/country/issues",
24+
"source": "https://github.com/tiny-blocks/country"
25+
},
2426
"config": {
2527
"sort-packages": true,
2628
"allow-plugins": {
@@ -38,25 +40,28 @@
3840
}
3941
},
4042
"require": {
41-
"php": "^8.1||^8.2",
42-
"tiny-blocks/value-object": "^2.0"
43+
"php": "^8.2",
44+
"tiny-blocks/value-object": "^2"
4345
},
4446
"require-dev": {
45-
"infection/infection": "^0.26",
46-
"phpmd/phpmd": "^2.13",
47-
"phpunit/phpunit": "^9.6",
48-
"squizlabs/php_codesniffer": "^3.7"
47+
"phpmd/phpmd": "^2.15",
48+
"phpstan/phpstan": "^1",
49+
"phpunit/phpunit": "^11",
50+
"infection/infection": "^0.29",
51+
"squizlabs/php_codesniffer": "^3.10"
4952
},
5053
"scripts": {
5154
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
5255
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --exclude /src/Alpha2Code.php,/src/Alpha3Code.php --ignore-violations-on-exit",
56+
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
5357
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
5458
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
5559
"test-no-coverage": "phpunit --no-coverage",
5660
"test-mutation-no-coverage": "infection --only-covered --min-msi=100 --threads=4",
5761
"review": [
5862
"@phpcs",
59-
"@phpmd"
63+
"@phpmd",
64+
"@phpstan"
6065
],
6166
"tests": [
6267
"@test",
@@ -65,6 +70,9 @@
6570
"tests-no-coverage": [
6671
"@test-no-coverage",
6772
"@test-mutation-no-coverage"
73+
],
74+
"tests-file-no-coverage": [
75+
"@test-no-coverage"
6876
]
6977
}
7078
}

infection.json.dist

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
{
22
"timeout": 10,
33
"testFramework": "phpunit",
4-
"tmpDir": "report/",
4+
"tmpDir": "report/infection/",
55
"source": {
66
"directories": [
77
"src"
88
]
99
},
1010
"logs": {
11-
"text": "report/logs/infection-text.log",
12-
"summary": "report/logs/infection-summary.log"
11+
"text": "report/infection/logs/infection-text.log",
12+
"summary": "report/infection/logs/infection-summary.log"
1313
},
1414
"mutators": {
15-
"@default": true,
16-
"MBString": false,
17-
"Ternary": false,
18-
"Identical": false,
19-
"DecrementInteger": false,
20-
"IncrementInteger": false,
21-
"PublicVisibility": false,
22-
"ProtectedVisibility": false
15+
"@default": true
2316
},
2417
"phpUnit": {
2518
"configDir": "",
2619
"customPath": "./vendor/bin/phpunit"
2720
}
28-
}
21+
}

phpstan.neon.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
paths:
3+
- src
4+
level: 9
5+
tmpDir: report/phpstan
6+
ignoreErrors:
7+
- '#value type specified in iterable type array#'
8+
reportUnmatchedIgnoredErrors: false

phpunit.xml

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
bootstrap="vendor/autoload.php"
4-
cacheResultFile="report/.phpunit.result.cache"
5-
backupGlobals="false"
6-
backupStaticAttributes="false"
3+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
74
colors="true"
8-
convertErrorsToExceptions="true"
9-
convertNoticesToExceptions="true"
10-
convertWarningsToExceptions="true"
11-
processIsolation="false"
12-
stopOnFailure="false"
13-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
5+
bootstrap="vendor/autoload.php"
6+
failOnRisky="true"
7+
failOnWarning="true"
8+
cacheDirectory=".phpunit.cache"
9+
beStrictAboutOutputDuringTests="true">
10+
11+
<source>
12+
<include>
13+
<directory>src</directory>
14+
</include>
15+
</source>
16+
1417
<testsuites>
1518
<testsuite name="default">
16-
<directory suffix="Test.php">tests</directory>
19+
<directory>tests</directory>
1720
</testsuite>
1821
</testsuites>
1922

2023
<coverage>
21-
<include>
22-
<directory suffix=".php">src</directory>
23-
</include>
24+
<report>
25+
<text outputFile="report/coverage.txt"/>
26+
<html outputDirectory="report/html/"/>
27+
<clover outputFile="report/coverage-clover.xml"/>
28+
</report>
2429
</coverage>
30+
31+
<logging>
32+
<junit outputFile="report/execution-result.xml"/>
33+
</logging>
34+
2535
</phpunit>

src/Alpha2Code.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TinyBlocks\Country;
46

57
use TinyBlocks\Country\Internal\AlphaCode;

src/Alpha3Code.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace TinyBlocks\Country;
46

57
use TinyBlocks\Country\Internal\AlphaCode;

0 commit comments

Comments
 (0)