Skip to content

Add property types #18551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion components/asset.rst
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ every day::

class DateVersionStrategy implements VersionStrategyInterface
{
private $version;
private \DateTimeInterface $version;

public function __construct()
{
Expand Down
6 changes: 3 additions & 3 deletions components/dependency_injection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ you want to make available as a service::

class Mailer
{
private $transport;
private string $transport;

public function __construct()
{
Expand All @@ -55,7 +55,7 @@ so this is passed into the constructor::
class Mailer
{
public function __construct(
private $transport,
private string $transport,
) {
}

Expand Down Expand Up @@ -124,7 +124,7 @@ it was only optional then you could use setter injection instead::

class NewsletterManager
{
private $mailer;
private \Mailer $mailer;

public function setMailer(\Mailer $mailer)
{
Expand Down
14 changes: 7 additions & 7 deletions components/property_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ it with ``get``. So the actual method becomes ``getFirstName()``::
// ...
class Person
{
private $firstName = 'Wouter';
private string $firstName = 'Wouter';

public function getFirstName()
{
Expand All @@ -140,8 +140,8 @@ getters, this means that you can do something like this::
// ...
class Person
{
private $author = true;
private $children = [];
private bool $author = true;
private array $children = [];

public function isAuthor()
{
Expand Down Expand Up @@ -233,7 +233,7 @@ The ``getValue()`` method can also use the magic ``__get()`` method::
// ...
class Person
{
private $children = [
private array $children = [
'Wouter' => [...],
];

Expand Down Expand Up @@ -263,7 +263,7 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert
// ...
class Person
{
private $children = [
private array $children = [
'wouter' => [...],
];

Expand Down Expand Up @@ -362,7 +362,7 @@ see `Enable other Features`_::
// ...
class Person
{
private $children = [];
private array $children = [];

public function __call($name, $args)
{
Expand Down Expand Up @@ -405,7 +405,7 @@ properties through *adder* and *remover* methods::
/**
* @var string[]
*/
private $children = [];
private array $children = [];

public function getChildren(): array
{
Expand Down
2 changes: 1 addition & 1 deletion components/property_info.rst
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ information from annotations of properties and methods, such as ``@var``,
* @param string $bar
*/
public function __construct(
private $bar,
private string $bar,
) {
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ always using this ``ReactPHPRunner``::

class ReactPHPRuntime extends GenericRuntime
{
private $port;
private int $port;

public function __construct(array $options)
{
Expand Down
48 changes: 24 additions & 24 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ Assume you have the following plain-old-PHP object::

class MyObj
{
public $foo;
public string $foo;

private $bar;
private string $bar;

public function getBar()
{
Expand Down Expand Up @@ -303,10 +303,10 @@ Then, create your groups definition:
class MyObj
{
#[Groups(['group1', 'group2'])]
public $foo;
public string $foo;

#[Groups(['group4'])]
public $anotherProperty;
public string $anotherProperty;

#[Groups(['group3'])]
public function getBar() // is* methods are also supported
Expand Down Expand Up @@ -449,10 +449,10 @@ Option 1: Using ``@Ignore`` Annotation

class MyClass
{
public $foo;
public string $foo;

#[Ignore]
public $bar;
public string $bar;
}

.. code-block:: yaml
Expand Down Expand Up @@ -1229,8 +1229,8 @@ You can change this behavior by setting the ``AbstractObjectNormalizer::SKIP_NUL
to ``true``::

$dummy = new class {
public $foo;
public $bar = 'notNull';
public ?string $foo = null;
public string $bar = 'notNull';
};

$normalizer = new ObjectNormalizer();
Expand Down Expand Up @@ -1305,8 +1305,8 @@ Circular references are common when dealing with entity relations::

class Organization
{
private $name;
private $members;
private string $name;
private array $members;

public function setName($name)
{
Expand All @@ -1331,10 +1331,10 @@ Circular references are common when dealing with entity relations::

class Member
{
private $name;
private $organization;
private string $name;
private Organization $organization;

public function setName($name)
public function setName(string $name)
{
$this->name = $name;
}
Expand Down Expand Up @@ -1404,12 +1404,12 @@ structure::

class MyObj
{
public $foo;
public string $foo;

/**
* @var self
*/
public $child;
public MyObj $child;
}

$level1 = new MyObj();
Expand Down Expand Up @@ -1437,7 +1437,7 @@ Here, we set it to 2 for the ``$child`` property:
class MyObj
{
#[MaxDepth(2)]
public $child;
public MyObj $child;

// ...
}
Expand Down Expand Up @@ -1499,10 +1499,10 @@ having unique identifiers::

class Foo
{
public $id;
public int $id;

#[MaxDepth(1)]
public $child;
public MyObj $child;
}

$level1 = new Foo();
Expand Down Expand Up @@ -1598,8 +1598,8 @@ context option::
class MyObj
{
public function __construct(
private $foo,
private $bar,
private string $foo,
private string $bar,
) {
}
}
Expand Down Expand Up @@ -1638,8 +1638,8 @@ parameter of the ``ObjectNormalizer``::

class ObjectOuter
{
private $inner;
private $date;
private ObjectInner $inner;
private \DateTimeInterface $date;

public function getInner()
{
Expand All @@ -1664,8 +1664,8 @@ parameter of the ``ObjectNormalizer``::

class ObjectInner
{
public $foo;
public $bar;
public string $foo;
public string $bar;
}

$normalizer = new ObjectNormalizer(null, null, null, new ReflectionExtractor());
Expand Down
2 changes: 1 addition & 1 deletion components/validator/metadata.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ the ``Author`` class has at least 3 characters::

class Author
{
private $firstName;
private string $firstName;

public static function loadValidatorMetadata(ClassMetadata $metadata)
{
Expand Down
5 changes: 2 additions & 3 deletions configuration/using_parameters_in_dic.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ be injected with this parameter via the extension as follows::

class Configuration implements ConfigurationInterface
{
private $debug;
private bool $debug;

public function __construct($debug)
public function __construct(private bool $debug)
{
$this->debug = (bool) $debug;
}

public function getConfigTreeBuilder()
Expand Down
5 changes: 1 addition & 4 deletions contributing/code/standards.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ short example containing most features described below::
{
public const SOME_CONST = 42;

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

/**
* @param $dummy some argument description
Expand Down
4 changes: 2 additions & 2 deletions controller/upload_file.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ add a PDF brochure for each product. To do so, add a new property called
// ...

#[ORM\Column(type: 'string')]
private $brochureFilename;
private string $brochureFilename;

public function getBrochureFilename(): string
{
Expand Down Expand Up @@ -238,7 +238,7 @@ logic to a separate service::
class FileUploader
{
public function __construct(
private $targetDirectory,
private string $targetDirectory,
private SluggerInterface $slugger,
) {
}
Expand Down
6 changes: 3 additions & 3 deletions doctrine/associations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ the ``Product`` entity (and getter & setter methods):
// ...

#[ORM\ManyToOne(targetEntity: Category::class, inversedBy: 'products')]
private $category;
private Category $category;

public function getCategory(): ?Category
{
Expand Down Expand Up @@ -220,7 +220,7 @@ class that will hold these objects:
// ...

#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category')]
private $products;
private array $products;

public function __construct()
{
Expand Down Expand Up @@ -588,7 +588,7 @@ that behavior, use the `orphanRemoval`_ option inside ``Category``:
// ...

#[ORM\OneToMany(targetEntity: Product::class, mappedBy: 'category', orphanRemoval: true)]
private $products;
private array $products;


Thanks to this, if the ``Product`` is removed from the ``Category``, it will be
Expand Down
2 changes: 1 addition & 1 deletion form/create_form_type_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ the database::
/**
* @var string The path - typically stored in the database
*/
private $path;
private string $path;

// ...

Expand Down
24 changes: 12 additions & 12 deletions form/inherit_data_option.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ entities, a ``Company`` and a ``Customer``::

class Company
{
private $name;
private $website;
private string $name;
private string $website;

private $address;
private $zipcode;
private $city;
private $country;
private string $address;
private string $zipcode;
private string $city;
private string $country;
}

.. code-block:: php
Expand All @@ -26,13 +26,13 @@ entities, a ``Company`` and a ``Customer``::

class Customer
{
private $firstName;
private $lastName;
private string $firstName;
private string $lastName;

private $address;
private $zipcode;
private $city;
private $country;
private string $address;
private string $zipcode;
private string $city;
private string $country;
}

As you can see, each entity shares a few of the same fields: ``address``,
Expand Down
2 changes: 1 addition & 1 deletion form/unit_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ make sure the ``FormRegistry`` uses the created instance::

class TestedTypeTest extends TypeTestCase
{
private $objectManager;
private MockObject|ObjectManager $objectManager;

protected function setUp(): void
{
Expand Down
Loading