Skip to content

Commit e25dfbe

Browse files
committed
Initial commit
0 parents  commit e25dfbe

File tree

13 files changed

+279
-0
lines changed

13 files changed

+279
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

code-of-conduct.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Our Pledge
2+
3+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
4+
5+
# Our Standards
6+
7+
Examples of behavior that contributes to creating a positive environment include:
8+
9+
* Using welcoming and inclusive language
10+
* Being respectful of differing viewpoints and experiences
11+
* Gracefully accepting constructive criticism
12+
* Focusing on what is best for the community
13+
* Showing empathy towards other community members
14+
15+
Examples of unacceptable behavior by participants include:
16+
17+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
18+
* Trolling, insulting/derogatory comments, and personal or political attacks
19+
* Public or private harassment
20+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
21+
* Other conduct which could reasonably be considered inappropriate in a professional setting
22+
23+
# Our Responsibilities
24+
25+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
26+
27+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
28+
29+
# Scope
30+
31+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
32+
33+
# Enforcement
34+
35+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at cgpitt@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
36+
37+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
38+
39+
# Attribution
40+
41+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
42+
43+
[homepage]: http://contributor-covenant.org
44+
[version]: http://contributor-covenant.org/version/1/4

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "pre/immutable-classes",
3+
"license": "MIT",
4+
"require": {
5+
"php": "^7.0",
6+
"pre/plugin": "^0.5"
7+
},
8+
"autoload": {
9+
"files": [
10+
"src/bootstrap.php"
11+
],
12+
"psr-4": {
13+
"Pre\\ImmutableClasses\\": "src"
14+
}
15+
},
16+
"require-dev": {
17+
"phpunit/phpunit": "^5.0"
18+
},
19+
"autoload-dev": {
20+
"psr-4": {
21+
"Pre\\ImmutableClasses\\": "tests"
22+
}
23+
},
24+
"minimum-stability": "dev",
25+
"prefer-stable": true
26+
}

license.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright Christopher Pitt
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
bootstrap="tests/bootstrap.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="false"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
syntaxCheck="false">
13+
<testsuites>
14+
<testsuite>
15+
<directory suffix="Test.php">tests</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

readme.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Pre Immutable Classes
2+
3+
Documentation can be found at [preprocess.io](https://preprocess.io/docs#immutable-classes).
4+
5+
## Versioning
6+
7+
This library follows [Semver](http://semver.org). According to Semver, you will be able to upgrade to any minor or patch version of this library without any breaking changes to the public API. Semver also requires that we clearly define the public API for this library.
8+
9+
All methods, with `public` visibility, are part of the public API. All other methods are not part of the public API. Where possible, we'll try to keep `protected` methods backwards-compatible in minor/patch versions, but if you're overriding methods then please test your work before upgrading.

src/ImmutableClassesTrait.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Pre\ImmutableClasses;
4+
5+
use InvalidArgumentException;
6+
7+
trait ImmutableClassesTrait
8+
{
9+
/**
10+
* @inheritdoc
11+
*
12+
* @param string $method
13+
* @param array $parameters
14+
*/
15+
public function __call($method, $parameters)
16+
{
17+
return $this->handleCallImmutableClassSetters($method, $parameters);
18+
}
19+
20+
/**
21+
* Handles calls to withProperty($value) methods.
22+
*
23+
* @param string $method
24+
* @param array $parameters
25+
*
26+
* @return static
27+
*/
28+
protected function handleCallImmutableClassSetters($method, $parameters)
29+
{
30+
if (stripos($method, "with") === 0) {
31+
if (count($parameters) < 1) {
32+
throw new InvalidArgumentException("{$method} expects a parameter, none given");
33+
}
34+
35+
$property = $this->camel(substr($method, 4));
36+
37+
if (property_exists($this, $property)) {
38+
return $this->cloneWith($property, $parameters[0]);
39+
}
40+
41+
throw new InvalidArgumentException("{$property} property does not exist");
42+
}
43+
}
44+
45+
/**
46+
* Replaces "ThisFormat" with "thisFormat".
47+
*
48+
* @return string
49+
*/
50+
private function camel($string)
51+
{
52+
return strtolower($string[0]) . substr($string, 1);
53+
}
54+
55+
/**
56+
* Returns a clone of this object, with the specified property changes to a new value.
57+
*
58+
* @param string $property
59+
* @param mixed $value
60+
*/
61+
protected function cloneWith($property, $value)
62+
{
63+
$clone = clone $this;
64+
$clone->$property = $value;
65+
66+
return $clone;
67+
}
68+
}

src/bootstrap.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
Pre\addMacroPath(__DIR__ . "/macros.pre");

src/macros.pre

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
macro {
4+
immutable class ·ns()·class {
5+
···body
6+
}
7+
} >> {
8+
class ·class {
9+
use \Pre\ImmutableClasses\ImmutableClassesTrait;
10+
11+
···body
12+
}
13+
}
14+
15+
macro {
16+
function __call(···parameters) {
17+
immutability;
18+
···body
19+
}
20+
} >> {
21+
function __call(···parameters) {
22+
if ($result = $this->handleCallImmutableClassSetters(···parameters)) {
23+
return $result;
24+
}
25+
26+
···body
27+
}
28+
}

tests/SpecTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Pre\ImmutableClasses;
4+
5+
use Pre\Testing\Runner;
6+
7+
class SpecTest extends Runner
8+
{
9+
protected function path(): string
10+
{
11+
return __DIR__ . "/specs";
12+
}
13+
}

0 commit comments

Comments
 (0)