Skip to content

Commit

Permalink
[minor] allow PHP 7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Jun 25, 2020
1 parent f52111a commit f1d1627
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.4]
php: [7.3, 7.4]
stability: [prefer-lowest, prefer-stable]
steps:
- name: Checkout code
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
],
"require": {
"php": ">=7.4",
"php": ">=7.3",
"doctrine/persistence": "^1.3.3",
"fzaninotto/faker": "^1.5",
"symfony/property-access": "^3.4|^4.4|^5.0"
Expand Down
3 changes: 2 additions & 1 deletion src/Bundle/Maker/MakeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*/
final class MakeFactory extends AbstractMaker
{
private ManagerRegistry $managerRegistry;
/** @var ManagerRegistry */
private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry)
{
Expand Down
11 changes: 8 additions & 3 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
*/
final class Configuration
{
private ManagerRegistry $managerRegistry;
private StoryManager $stories;
private Faker\Generator $faker;
/** @var ManagerRegistry */
private $managerRegistry;

/** @var StoryManager */
private $stories;

/** @var Faker\Generator */
private $faker;

/** @var callable */
private $instantiator;
Expand Down
38 changes: 28 additions & 10 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@
*/
class Factory
{
private static ?Configuration $configuration = null;
/** @var Configuration|null */
private static $configuration;

private string $class;
/** @var string */
private $class;

/** @var callable|null */
private $instantiator;

private bool $persist = true;
/** @var bool */
private $persist = true;

/** @var array<array|callable> */
private array $attributeSet = [];
private $attributeSet = [];

/** @var callable[] */
private array $beforeInstantiate = [];
private $beforeInstantiate = [];

/** @var callable[] */
private array $afterInstantiate = [];
private $afterInstantiate = [];

/** @var callable[] */
private array $afterPersist = [];
private $afterPersist = [];

/**
* @param array|callable $defaultAttributes
Expand Down Expand Up @@ -68,7 +71,12 @@ final public function create($attributes = []): Proxy
*/
final public function createMany(int $number, $attributes = []): array
{
return \array_map(fn() => $this->create($attributes), \array_fill(0, $number, null));
return \array_map(
function() use ($attributes) {
return $this->create($attributes);
},
\array_fill(0, $number, null)
);
}

public function withoutPersisting(): self
Expand Down Expand Up @@ -187,7 +195,12 @@ private function instantiate($attributes): object
}

// filter each attribute to convert proxies and factories to objects
$attributes = \array_map(fn($value) => $this->normalizeAttribute($value), $attributes);
$attributes = \array_map(
function($value) {
return $this->normalizeAttribute($value);
},
$attributes,
);

// instantiate the object with the users instantiator or if not set, the default instantiator
$object = ($this->instantiator ?? self::configuration()->instantiator())($attributes, $this->class);
Expand All @@ -212,7 +225,12 @@ private function normalizeAttribute($value)

if (\is_array($value)) {
// possible OneToMany/ManyToMany relationship
return \array_map(fn($value) => $this->normalizeAttribute($value), $value);
return \array_map(
function($value) {
return $this->normalizeAttribute($value);
},
$value
);
}

if (!$value instanceof self) {
Expand Down
14 changes: 10 additions & 4 deletions src/Instantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
*/
final class Instantiator
{
private static ?PropertyAccessor $propertyAccessor = null;
/** @var PropertyAccessor|null */
private static $propertyAccessor;

private bool $withoutConstructor = false;
private bool $allowExtraAttributes = false;
private bool $alwaysForceProperties = false;
/** @var bool */
private $withoutConstructor = false;

/** @var bool */
private $allowExtraAttributes = false;

/** @var bool */
private $alwaysForceProperties = false;

public function __invoke(array $attributes, string $class): object
{
Expand Down
19 changes: 15 additions & 4 deletions src/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@
*/
final class Proxy
{
private object $object;
private string $class;
private bool $autoRefresh = false;
private bool $persisted = false;
/** @var object */
private $object;

/** @var string */
private $class;

/** @var bool */
private $autoRefresh = false;

/** @var bool */
private $persisted = false;

public function __construct(object $object)
{
Expand Down Expand Up @@ -49,6 +56,10 @@ public function __isset(string $name): bool
public function __toString(): string
{
if (!\method_exists($this->object, '__toString')) {
if (\PHP_VERSION_ID < 70400) {
return '(no __toString)';
}

throw new \RuntimeException(\sprintf('Proxied object "%s" cannot be converted to a string.', $this->class));
}

Expand Down
3 changes: 2 additions & 1 deletion src/RepositoryProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
*/
final class RepositoryProxy implements ObjectRepository
{
private ObjectRepository $repository;
/** @var ObjectRepository */
private $repository;

public function __construct(ObjectRepository $repository)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Story.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
abstract class Story
{
/** @var array<string, Proxy> */
private array $objects = [];
private $objects = [];

final public function __call(string $method, array $arguments)
{
Expand Down
6 changes: 3 additions & 3 deletions src/StoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
final class StoryManager
{
/** @var array<string, Story> */
private static array $globalInstances = [];
private static $globalInstances = [];

/** @var array<string, Story> */
private static array $instances = [];
private static $instances = [];

/** @var Story[] */
private iterable $stories;
private $stories;

/**
* @param Story[] $stories
Expand Down
3 changes: 2 additions & 1 deletion src/Test/DatabaseResetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*/
final class DatabaseResetter
{
private static bool $hasBeenReset = false;
/** @var bool */
private static $hasBeenReset = false;

public static function hasBeenReset(): bool
{
Expand Down
12 changes: 9 additions & 3 deletions src/Test/TestState.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ final class TestState
{
/** @var callable|null */
private static $instantiator;
private static ?Faker\Generator $faker = null;
private static bool $useBundle = true;
private static array $globalStates = [];

/** @var Faker\Generator|null */
private static $faker;

/** @var bool */
private static $useBundle = true;

/** @var callable[] */
private static $globalStates = [];

public static function setInstantiator(callable $instantiator): void
{
Expand Down
3 changes: 2 additions & 1 deletion tests/Fixtures/Stories/ServiceStory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
*/
final class ServiceStory extends Story
{
private Service $service;
/** @var Service */
private $service;

public function __construct(Service $service)
{
Expand Down
21 changes: 18 additions & 3 deletions tests/Functional/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public function one_to_many_relationship(): void
],
]);

$posts = \array_map(fn($post) => $post->getTitle(), $category->getPosts()->toArray());
$posts = \array_map(
static function($post) {
return $post->getTitle();
},
$category->getPosts()->toArray()
);

$this->assertCount(2, $posts);
$this->assertContains('Post A', $posts);
Expand All @@ -63,7 +68,12 @@ public function many_to_many_relationship(): void
],
]);

$tags = \array_map(fn($tag) => $tag->getName(), $post->getTags()->toArray());
$tags = \array_map(
static function($tag) {
return $tag->getName();
},
$post->getTags()->toArray()
);

$this->assertCount(2, $tags);
$this->assertContains('Tag A', $tags);
Expand All @@ -83,7 +93,12 @@ public function many_to_many_reverse_relationship(): void
],
]);

$posts = \array_map(fn($post) => $post->getTitle(), $tag->getPosts()->toArray());
$posts = \array_map(
static function($post) {
return $post->getTitle();
},
$tag->getPosts()->toArray()
);

$this->assertCount(2, $posts);
$this->assertContains('Post A', $posts);
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/ModelFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function can_find_random_set_of_objects(): void
$objects = CategoryFactory::randomSet(3);

$this->assertCount(3, $objects);
$this->assertCount(3, \array_unique(\array_map(fn($category) => $category->getId(), $objects)));
$this->assertCount(3, \array_unique(\array_map(static function($category) { return $category->getId(); }, $objects)));
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Functional/ProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function can_convert_to_string_if_wrapped_object_can(): void

/**
* @test
* @requires PHP >= 7.4
*/
public function cannot_convert_to_string_if_underlying_object_cant(): void
{
Expand All @@ -65,6 +66,15 @@ public function cannot_convert_to_string_if_underlying_object_cant(): void
(string) CategoryFactory::new()->create();
}

/**
* @test
* @requires PHP < 7.4
*/
public function on_php_versions_less_than_7_4_if_underlying_object_is_missing_to_string_proxy_to_string_returns_note(): void
{
$this->assertSame('(no __toString)', (string) CategoryFactory::new()->create());
}

/**
* @test
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/RepositoryProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public function can_find_random_set_of_objects(): void
$objects = repository(Category::class)->randomSet(3);

$this->assertCount(3, $objects);
$this->assertCount(3, \array_unique(\array_map(fn($category) => $category->getId(), $objects)));
$this->assertCount(3, \array_unique(\array_map(static function($category) { return $category->getId(); }, $objects)));
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/Unit/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ final class FactoryTest extends UnitTestCase
public function can_instantiate_object(): void
{
$attributeArray = ['title' => 'title', 'body' => 'body'];
$attributeCallback = fn(Faker\Generator $faker) => ['title' => 'title', 'body' => 'body'];
$attributeCallback = static function(Faker\Generator $faker) {
return ['title' => 'title', 'body' => 'body'];
};

$this->assertSame('title', (new Factory(Post::class, $attributeArray))->withoutPersisting()->create()->getTitle());
$this->assertSame('title', (new Factory(Post::class))->withoutPersisting()->create($attributeArray)->getTitle());
Expand All @@ -38,7 +40,9 @@ public function can_instantiate_object(): void
public function can_instantiate_many_objects(): void
{
$attributeArray = ['title' => 'title', 'body' => 'body'];
$attributeCallback = fn(Faker\Generator $faker) => ['title' => 'title', 'body' => 'body'];
$attributeCallback = static function(Faker\Generator $faker) {
return ['title' => 'title', 'body' => 'body'];
};

$objects = (new Factory(Post::class, $attributeArray))->withoutPersisting()->createMany(3);

Expand Down
3 changes: 2 additions & 1 deletion tests/UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
abstract class UnitTestCase extends TestCase
{
protected ?Configuration $configuration = null;
/** @var Configuration|null */
protected $configuration;

/**
* @before
Expand Down

0 comments on commit f1d1627

Please sign in to comment.