Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8f7ccd

Browse files
committed
Fix: Require to pass name to ContainerBuilder::add()
1 parent ea48cdd commit f8f7ccd

File tree

4 files changed

+8
-60
lines changed

4 files changed

+8
-60
lines changed

roave-bc-check.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ parameters:
1616
- '#\[BC\] REMOVED: Class Faker\\Extension\\Container has been deleted#'
1717
- '#\[BC\] CHANGED: The parameter \$container of Faker\\Generator\#\_\_construct\(\) changed from Psr\\Container\\ContainerInterface\|null to a non-contravariant Faker\\Container\\ContainerInterface\|null#'
1818
- '#\[BC\] CHANGED: The parameter \$container of Faker\\Generator\#\_\_construct\(\) changed from Psr\\Container\\ContainerInterface\|null to Faker\\Container\\ContainerInterface\|null#'
19+
- '#\[BC\] CHANGED: The number of required arguments for Faker\\Container\\ContainerBuilder\#add\(\) increased from 1 to 2#'
20+
- '#\[BC\] CHANGED: The parameter \$name of Faker\\Container\\ContainerBuilder\#add\(\) changed from string\|null to a non-contravariant string#'

src/Faker/Container/ContainerBuilder.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class ContainerBuilder
2929
*
3030
* @throws \InvalidArgumentException
3131
*/
32-
public function add($value, string $name = null): self
32+
public function add($value, string $name): self
3333
{
3434
if (!is_string($value) && !is_callable($value) && !is_object($value)) {
3535
throw new \InvalidArgumentException(sprintf(
@@ -38,19 +38,6 @@ public function add($value, string $name = null): self
3838
));
3939
}
4040

41-
if ($name === null) {
42-
if (is_string($value)) {
43-
$name = $value;
44-
} elseif (is_object($value)) {
45-
$name = get_class($value);
46-
} else {
47-
throw new \InvalidArgumentException(sprintf(
48-
'Second argument to "%s::add()" is required not passing a string or object as first argument',
49-
self::class,
50-
));
51-
}
52-
}
53-
5441
$this->definitions[$name] = $value;
5542

5643
return $this;

test/Faker/Extension/ContainerBuilderTest.php

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Faker\Container\ContainerBuilder;
88
use Faker\Container\ContainerInterface;
99
use Faker\Core\File;
10-
use Faker\Extension\Extension;
1110
use PHPUnit\Framework\TestCase;
1211

1312
/**
@@ -30,7 +29,7 @@ public function testAddRejectsInvalidValue($value): void
3029
ContainerBuilder::class,
3130
));
3231

33-
$containerBuilder->add($value);
32+
$containerBuilder->add($value, 'foo');
3433
}
3534

3635
/**
@@ -59,46 +58,6 @@ public function provideInvalidValue(): \Generator
5958
}
6059
}
6160

62-
public function testAddRejectsNameWhenValueIsCallableAndNameIsNull(): void
63-
{
64-
$value = [
65-
new class() {
66-
public static function create(): Extension
67-
{
68-
return new class() implements Extension {
69-
};
70-
}
71-
},
72-
'create',
73-
];
74-
75-
$containerBuilder = new ContainerBuilder();
76-
77-
$this->expectException(\InvalidArgumentException::class);
78-
$this->expectExceptionMessage(sprintf(
79-
'Second argument to "%s::add()" is required not passing a string or object as first argument',
80-
ContainerBuilder::class,
81-
));
82-
83-
$containerBuilder->add($value);
84-
}
85-
86-
public function testAddAcceptsValueWhenItIsAnObjectAndNameIsNull(): void
87-
{
88-
$value = new class() implements Extension {};
89-
90-
$name = get_class($value);
91-
92-
$containerBuilder = new ContainerBuilder();
93-
94-
$containerBuilder->add($value);
95-
96-
$container = $containerBuilder->build();
97-
98-
self::assertTrue($container->has($name));
99-
self::assertSame($value, $container->get($name));
100-
}
101-
10261
public function testBuildEmpty(): void
10362
{
10463
$builder = new ContainerBuilder();
@@ -112,7 +71,7 @@ public function testBuild(): void
11271
{
11372
$builder = new ContainerBuilder();
11473

115-
$builder->add(File::class);
74+
$builder->add(File::class, 'foo');
11675

11776
$container = $builder->build();
11877

@@ -123,8 +82,8 @@ public function testBuildWithDuplicates(): void
12382
{
12483
$builder = new ContainerBuilder();
12584

126-
$builder->add(File::class);
127-
$builder->add(File::class);
85+
$builder->add(File::class, 'foo');
86+
$builder->add(File::class, 'foo');
12887

12988
$container = $builder->build();
13089

test/Faker/GeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testFormatterCallsGenerator(): void
145145
public function testFormatterCallsExtension(): void
146146
{
147147
$builder = new ContainerBuilder();
148-
$builder->add(Blood::class);
148+
$builder->add(Blood::class, Blood::class);
149149
$faker = new Generator($builder->build());
150150

151151
$output = $faker->format('Faker\Core\Blood->bloodType');

0 commit comments

Comments
 (0)