Skip to content

Commit

Permalink
Update the code style a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasnoback committed Feb 3, 2020
1 parent 39dba2a commit 9482a0b
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Matthias Noback
Copyright (c) Matthias Noback

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class OrderSeats
And use it like this:

```php
$form = $this->factory->create(new OrderSeatsFormType());
$form = $this->factory->create(OrderSeatsFormType::class);
$form->submit($request);
$command = $form->getData();
$command->id = Uuid::uuid4();
Expand Down Expand Up @@ -74,12 +74,12 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
[ico-version]: https://img.shields.io/packagist/v/matthiasnoback/convenient-immutability.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-coveralls]: https://img.shields.io/coveralls/matthiasnoback/convenient-immutability.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/matthiasnoback/convenient-immutability.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/matthiasnoback/convenient-immutability.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/matthiasnoback/convenient-immutability.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/matthiasnoback/convenient-immutability
[link-coveralls]: https://coveralls.io/github/matthiasnoback/convenient-immutability
[link-travis]: https://travis-ci.org/matthiasnoback/convenient-immutability
[link-downloads]: https://packagist.org/packages/matthiasnoback/convenient-immutability
[link-author]: https://github.com/matthiasnoback
[link-contributors]: ../../contributors
[link-contributors]: ../../contributors
14 changes: 9 additions & 5 deletions src/Immutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace ConvenientImmutability;

use LogicException;
use ReflectionObject;
use const PHP_VERSION_ID;

trait Immutable
{
private $_defaultValues = [];
Expand All @@ -17,15 +21,15 @@ trait Immutable
public function __construct()
{
// take over all user-defined non-static properties
foreach ((new \ReflectionObject($this))->getProperties() as $property) {
foreach ((new ReflectionObject($this))->getProperties() as $property) {
$propertyName = $property->getName();

if (isset(self::$_doNotTakeOverProperties[$propertyName]) || $property->isStatic()) {
continue;
}

$this->_userDefinedProperties[$propertyName] = true;
if (\PHP_VERSION_ID < 70400 || $property->isInitialized($this)) {
if (PHP_VERSION_ID < 70400 || $property->isInitialized($this)) {
$this->_defaultValues[$propertyName] = $property->getValue($this);
}
unset($this->{$propertyName});
Expand All @@ -35,11 +39,11 @@ public function __construct()
final public function __set($name, $value)
{
if (!isset($this->_userDefinedProperties[$name])) {
throw new \LogicException('Unknown property "' . $name . '"');
throw new LogicException('Unknown property "' . $name . '"');
}

if (array_key_exists($name, $this->_userDefinedValues)) {
throw new \LogicException('You can not overwrite the value for property "' . $name . '"');
throw new LogicException('You can not overwrite the value for property "' . $name . '"');
}

$this->_userDefinedValues[$name] = $value;
Expand All @@ -48,7 +52,7 @@ final public function __set($name, $value)
final public function __get($name)
{
if (!isset($this->_userDefinedProperties[$name])) {
throw new \LogicException('Unknown property "' . $name . '"');
throw new LogicException('Unknown property "' . $name . '"');
}

if (array_key_exists($name, $this->_userDefinedValues)) {
Expand Down
4 changes: 2 additions & 2 deletions test/FormIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
use Ramsey\Uuid\Uuid;
use Symfony\Component\Form\Test\TypeTestCase;

class FormIntegrationTest extends TypeTestCase
final class FormIntegrationTest extends TypeTestCase
{
/**
* @test
*/
public function it_works_with_a_standard_use_case_for_command_objects()
public function it_works_with_a_standard_use_case_for_command_objects(): void
{
$id = Uuid::uuid4();
$form = $this->factory->create(OrderSeatsFormType::class);
Expand Down
18 changes: 9 additions & 9 deletions test/ImmutableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
use LogicException;
use PHPUnit\Framework\TestCase;

class ImmutableTraitTest extends TestCase
final class ImmutableTraitTest extends TestCase
{
/**
* @return array
*/
public function objectProvider()
public function objectProvider(): array
{
$foo = new Foo();

Expand All @@ -32,7 +32,7 @@ public function objectProvider()
* @test
* @dataProvider objectProvider
*/
public function it_allows_one_assignment_per_property(Foo $foo)
public function it_allows_one_assignment_per_property(Foo $foo): void
{
$foo->bar = 1;
$foo->baz = 2;
Expand All @@ -45,7 +45,7 @@ public function it_allows_one_assignment_per_property(Foo $foo)
* @test
* @dataProvider objectProvider
*/
public function it_fails_when_a_property_gets_reassigned(Foo $foo)
public function it_fails_when_a_property_gets_reassigned(Foo $foo): void
{
$foo->bar = 1;

Expand All @@ -59,7 +59,7 @@ public function it_fails_when_a_property_gets_reassigned(Foo $foo)
* @test
* @dataProvider objectProvider
*/
public function it_fails_to_assign_values_to_undefined_properties(Foo $foo)
public function it_fails_to_assign_values_to_undefined_properties(Foo $foo): void
{
$this->expectException(LogicException::class);
$foo->bang = 1;
Expand All @@ -69,7 +69,7 @@ public function it_fails_to_assign_values_to_undefined_properties(Foo $foo)
* @test
* @dataProvider objectProvider
*/
public function it_fails_to_retrieve_values_from_undefined_properties(Foo $foo)
public function it_fails_to_retrieve_values_from_undefined_properties(Foo $foo): void
{
$this->expectException(LogicException::class);
$foo->bang;
Expand All @@ -79,7 +79,7 @@ public function it_fails_to_retrieve_values_from_undefined_properties(Foo $foo)
* @test
* @dataProvider objectProvider
*/
public function it_returns_null_for_properties_that_have_no_assigned_value(Foo $foo)
public function it_returns_null_for_properties_that_have_no_assigned_value(Foo $foo): void
{
$this->assertSame(null, $foo->bar);
}
Expand All @@ -88,7 +88,7 @@ public function it_returns_null_for_properties_that_have_no_assigned_value(Foo $
* @test
* @dataProvider objectProvider
*/
public function it_returns_the_default_attribute_value_of_properties_that_have_no_assigned_value(Foo $foo)
public function it_returns_the_default_attribute_value_of_properties_that_have_no_assigned_value(Foo $foo): void
{
$this->assertSame([], $foo->bazzes);
}
Expand All @@ -97,7 +97,7 @@ public function it_returns_the_default_attribute_value_of_properties_that_have_n
* @test
* @dataProvider objectProvider
*/
public function it_should_have_the_same_values_after_deserialization(Foo $foo)
public function it_should_have_the_same_values_after_deserialization(Foo $foo): void
{
$foo->bar = 'bar';
$foo->baz = 1;
Expand Down
2 changes: 1 addition & 1 deletion test/Resources/OrderSeats.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use ConvenientImmutability\Immutable;

class OrderSeats
final class OrderSeats
{
use Immutable;

Expand Down
2 changes: 1 addition & 1 deletion test/Resources/OrderSeatsFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class OrderSeatsFormType extends AbstractType
final class OrderSeatsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
Expand Down

0 comments on commit 9482a0b

Please sign in to comment.