Skip to content

Commit

Permalink
refactor: Adds segregation for immutability with an Immutable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavofreze committed Oct 4, 2024
1 parent af5a37e commit 684212f
Show file tree
Hide file tree
Showing 25 changed files with 262 additions and 434 deletions.
14 changes: 14 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/tests export-ignore
/vendor export-ignore

/LICENSE export-ignore
/Makefile export-ignore
/README.md export-ignore
/phpmd.xml export-ignore
/phpunit.xml export-ignore
/phpstan.neon.dist export-ignore
/infection.json.dist export-ignore

/.github export-ignore
/.gitignore export-ignore
/.gitattributes export-ignore
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Use PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

Expand All @@ -33,6 +38,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Use PHP 8.2
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Install dependencies
run: composer update --no-progress --optimize-autoloader

Expand Down
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
DOCKER_RUN = docker run --rm -it --net=host -v ${PWD}:/app -w /app gustavofreze/php:8.2

.PHONY: configure test test-no-coverage review show-reports clean
.PHONY: configure test test-file test-no-coverage review show-reports clean

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

test: review
test:
@${DOCKER_RUN} composer tests

test-no-coverage: review
test-file:
@${DOCKER_RUN} composer tests-file-no-coverage ${FILE}

test-no-coverage:
@${DOCKER_RUN} composer tests-no-coverage

review:
Expand All @@ -19,4 +22,4 @@ show-reports:

clean:
@sudo chown -R ${USER}:${USER} ${PWD}
@rm -rf report vendor
@rm -rf report vendor .phpunit.cache
21 changes: 13 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
"keywords": [
"vo",
"psr",
"psr-4",
"psr-12",
"tiny-blocks",
"value-object"
],
Expand All @@ -20,6 +18,10 @@
"homepage": "https://github.com/gustavofreze"
}
],
"support": {
"issues": "https://github.com/tiny-blocks/value-object/issues",
"source": "https://github.com/tiny-blocks/value-object"
},
"config": {
"sort-packages": true,
"allow-plugins": {
Expand All @@ -37,24 +39,27 @@
}
},
"require": {
"php": "^8.1||^8.2"
"php": "^8.2"
},
"require-dev": {
"infection/infection": "^0.26",
"phpmd/phpmd": "^2.13",
"phpunit/phpunit": "^9.6",
"squizlabs/php_codesniffer": "^3.7"
"phpmd/phpmd": "^2.15",
"phpunit/phpunit": "^11",
"phpstan/phpstan": "^1",
"infection/infection": "^0.29",
"squizlabs/php_codesniffer": "^3.10"
},
"scripts": {
"phpcs": "phpcs --standard=PSR12 --extensions=php ./src",
"phpmd": "phpmd ./src text phpmd.xml --suffixes php --ignore-violations-on-exit",
"phpstan": "phpstan analyse -c phpstan.neon.dist --quiet --no-progress",
"test": "phpunit --log-junit=report/coverage/junit.xml --coverage-xml=report/coverage/coverage-xml --coverage-html=report/coverage/coverage-html tests",
"test-mutation": "infection --only-covered --logger-html=report/coverage/mutation-report.html --coverage=report/coverage --min-msi=100 --min-covered-msi=100 --threads=4",
"test-no-coverage": "phpunit --no-coverage",
"test-mutation-no-coverage": "infection --only-covered --min-msi=100 --threads=4",
"review": [
"@phpcs",
"@phpmd"
"@phpmd",
"@phpstan"
],
"tests": [
"@test",
Expand Down
12 changes: 5 additions & 7 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
{
"timeout": 10,
"testFramework": "phpunit",
"tmpDir": "report/",
"tmpDir": "report/infection/",
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "report/logs/infection-text.log",
"summary": "report/logs/infection-summary.log"
"text": "report/infection/logs/infection-text.log",
"summary": "report/infection/logs/infection-summary.log"
},
"mutators": {
"@default": true,
"PublicVisibility": false,
"ProtectedVisibility": false
"@default": true
},
"phpUnit": {
"configDir": "",
"customPath": "./vendor/bin/phpunit"
}
}
}
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
paths:
- src
level: 9
tmpDir: report/phpstan
ignoreErrors:
- '#return type has no value type specified in iterable type array#'
reportUnmatchedIgnoredErrors: false
38 changes: 24 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
cacheResultFile="report/.phpunit.result.cache"
backupGlobals="false"
backupStaticAttributes="false"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
cacheDirectory=".phpunit.cache"
beStrictAboutOutputDuringTests="true">

<source>
<include>
<directory>src</directory>
</include>
</source>

<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<text outputFile="report/coverage.txt"/>
<html outputDirectory="report/html/"/>
<clover outputFile="report/coverage-clover.xml"/>
</report>
</coverage>

<logging>
<junit outputFile="report/execution-result.xml"/>
</logging>

</phpunit>
40 changes: 40 additions & 0 deletions src/Immutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Vo;

use TinyBlocks\Vo\Internal\Exceptions\InvalidProperty;
use TinyBlocks\Vo\Internal\Exceptions\PropertyCannotBeChanged;
use TinyBlocks\Vo\Internal\Exceptions\PropertyCannotBeDeactivated;

/**
* Defines immutability by restricting property modification.
*/
interface Immutable
{
/**
* Does not allow to get unknown property.
* @param mixed $key
* @return void
* @throws InvalidProperty Get unknown property.
*/
public function __get(mixed $key): void;

/**
* Does not allow injection of unknown property.
* @param mixed $key
* @param mixed $value
* @return void
* @throws PropertyCannotBeChanged If set property.
*/
public function __set(mixed $key, mixed $value): void;

/**
* Does not allow properties to be disabled.
* @param mixed $key
* @return void
* @throws PropertyCannotBeDeactivated If disable property.
*/
public function __unset(mixed $key): void;
}
2 changes: 2 additions & 0 deletions src/Internal/Exceptions/InvalidProperty.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Vo\Internal\Exceptions;

use RuntimeException;
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Exceptions/PropertyCannotBeChanged.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Vo\Internal\Exceptions;

use RuntimeException;
Expand Down
2 changes: 2 additions & 0 deletions src/Internal/Exceptions/PropertyCannotBeDeactivated.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Vo\Internal\Exceptions;

use RuntimeException;
Expand Down
33 changes: 3 additions & 30 deletions src/ValueObject.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php

namespace TinyBlocks\Vo;
declare(strict_types=1);

use TinyBlocks\Vo\Internal\Exceptions\InvalidProperty;
use TinyBlocks\Vo\Internal\Exceptions\PropertyCannotBeChanged;
use TinyBlocks\Vo\Internal\Exceptions\PropertyCannotBeDeactivated;
namespace TinyBlocks\Vo;

/**
* A Value Object is an immutable type that is only distinguishable by the state of its properties, that is,
Expand All @@ -13,7 +11,7 @@
*
* @see http://martinfowler.com/bliki/ValueObject.html
*/
interface ValueObject
interface ValueObject extends Immutable
{
/**
* Returns object values.
Expand All @@ -27,29 +25,4 @@ public function values(): array;
* @return bool
*/
public function equals(ValueObject $other): bool;

/**
* Does not allow to get unknown property.
* @param mixed $key
* @return void
* @throws InvalidProperty — Get unknown property.
*/
public function __get(mixed $key): void;

/**
* Does not allow injection of unknown property.
* @param mixed $key
* @param mixed $value
* @return void
* @throws PropertyCannotBeChanged — If set property.
*/
public function __set(mixed $key, mixed $value): void;

/**
* Does not allow properties to be disabled.
* @param mixed $key
* @return void
* @throws PropertyCannotBeDeactivated — If disable property.
*/
public function __unset(mixed $key): void;
}
2 changes: 2 additions & 0 deletions src/ValueObjectAdapter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace TinyBlocks\Vo;

use TinyBlocks\Vo\Internal\Exceptions\InvalidProperty;
Expand Down
Loading

0 comments on commit 684212f

Please sign in to comment.