diff --git a/components/asset.rst b/components/asset.rst index 2c95a35589e..8ffdbd7e41d 100644 --- a/components/asset.rst +++ b/components/asset.rst @@ -203,7 +203,7 @@ every day:: class DateVersionStrategy implements VersionStrategyInterface { - private $version; + private \DateTimeInterface $version; public function __construct() { diff --git a/components/dependency_injection.rst b/components/dependency_injection.rst index 6d8672feeef..45ecc6dc194 100644 --- a/components/dependency_injection.rst +++ b/components/dependency_injection.rst @@ -31,7 +31,7 @@ you want to make available as a service:: class Mailer { - private $transport; + private string $transport; public function __construct() { @@ -55,7 +55,7 @@ so this is passed into the constructor:: class Mailer { public function __construct( - private $transport, + private string $transport, ) { } @@ -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) { diff --git a/components/property_access.rst b/components/property_access.rst index 3364b667a5c..bf1453d17ee 100644 --- a/components/property_access.rst +++ b/components/property_access.rst @@ -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() { @@ -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() { @@ -233,7 +233,7 @@ The ``getValue()`` method can also use the magic ``__get()`` method:: // ... class Person { - private $children = [ + private array $children = [ 'Wouter' => [...], ]; @@ -263,7 +263,7 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert // ... class Person { - private $children = [ + private array $children = [ 'wouter' => [...], ]; @@ -362,7 +362,7 @@ see `Enable other Features`_:: // ... class Person { - private $children = []; + private array $children = []; public function __call($name, $args) { @@ -405,7 +405,7 @@ properties through *adder* and *remover* methods:: /** * @var string[] */ - private $children = []; + private array $children = []; public function getChildren(): array { diff --git a/components/property_info.rst b/components/property_info.rst index 6aa2210a728..37c425d85df 100644 --- a/components/property_info.rst +++ b/components/property_info.rst @@ -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, ) { } } diff --git a/components/runtime.rst b/components/runtime.rst index 956e731c5ab..8f07968e5ca 100644 --- a/components/runtime.rst +++ b/components/runtime.rst @@ -440,7 +440,7 @@ always using this ``ReactPHPRunner``:: class ReactPHPRuntime extends GenericRuntime { - private $port; + private int $port; public function __construct(array $options) { diff --git a/components/serializer.rst b/components/serializer.rst index 26336dc5f9b..33fc71e4b69 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -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() { @@ -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 @@ -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 @@ -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(); @@ -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) { @@ -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; } @@ -1404,12 +1404,12 @@ structure:: class MyObj { - public $foo; + public string $foo; /** * @var self */ - public $child; + public MyObj $child; } $level1 = new MyObj(); @@ -1437,7 +1437,7 @@ Here, we set it to 2 for the ``$child`` property: class MyObj { #[MaxDepth(2)] - public $child; + public MyObj $child; // ... } @@ -1499,10 +1499,10 @@ having unique identifiers:: class Foo { - public $id; + public int $id; #[MaxDepth(1)] - public $child; + public MyObj $child; } $level1 = new Foo(); @@ -1598,8 +1598,8 @@ context option:: class MyObj { public function __construct( - private $foo, - private $bar, + private string $foo, + private string $bar, ) { } } @@ -1638,8 +1638,8 @@ parameter of the ``ObjectNormalizer``:: class ObjectOuter { - private $inner; - private $date; + private ObjectInner $inner; + private \DateTimeInterface $date; public function getInner() { @@ -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()); diff --git a/components/validator/metadata.rst b/components/validator/metadata.rst index 07ee9c52d79..5a88dab76ed 100755 --- a/components/validator/metadata.rst +++ b/components/validator/metadata.rst @@ -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) { diff --git a/configuration/using_parameters_in_dic.rst b/configuration/using_parameters_in_dic.rst index 05008114e01..852b06506ce 100644 --- a/configuration/using_parameters_in_dic.rst +++ b/configuration/using_parameters_in_dic.rst @@ -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() diff --git a/contributing/code/standards.rst b/contributing/code/standards.rst index 69d0a2c16b3..7d16e0f9d0b 100644 --- a/contributing/code/standards.rst +++ b/contributing/code/standards.rst @@ -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 diff --git a/controller/upload_file.rst b/controller/upload_file.rst index 9bcc17cd7cd..dc132e3d021 100644 --- a/controller/upload_file.rst +++ b/controller/upload_file.rst @@ -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 { @@ -238,7 +238,7 @@ logic to a separate service:: class FileUploader { public function __construct( - private $targetDirectory, + private string $targetDirectory, private SluggerInterface $slugger, ) { } diff --git a/doctrine/associations.rst b/doctrine/associations.rst index e1cd113ae22..b17877c4bdf 100644 --- a/doctrine/associations.rst +++ b/doctrine/associations.rst @@ -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 { @@ -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() { @@ -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 diff --git a/form/create_form_type_extension.rst b/form/create_form_type_extension.rst index 43e6b7f198e..7f40b9decc9 100644 --- a/form/create_form_type_extension.rst +++ b/form/create_form_type_extension.rst @@ -107,7 +107,7 @@ the database:: /** * @var string The path - typically stored in the database */ - private $path; + private string $path; // ... diff --git a/form/inherit_data_option.rst b/form/inherit_data_option.rst index 64001ba074d..19b14b27bcd 100644 --- a/form/inherit_data_option.rst +++ b/form/inherit_data_option.rst @@ -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 @@ -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``, diff --git a/form/unit_testing.rst b/form/unit_testing.rst index bcd82a1ee38..048a3c7c0d0 100644 --- a/form/unit_testing.rst +++ b/form/unit_testing.rst @@ -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 { diff --git a/form/use_empty_data.rst b/form/use_empty_data.rst index 85d6d750a25..5387820693b 100644 --- a/form/use_empty_data.rst +++ b/form/use_empty_data.rst @@ -51,7 +51,7 @@ that constructor with no arguments:: class BlogType extends AbstractType { public function __construct( - private $someDependency, + private object $someDependency, ) { } // ... diff --git a/form/validation_group_service_resolver.rst b/form/validation_group_service_resolver.rst index da07585b511..82a6f65d6ec 100644 --- a/form/validation_group_service_resolver.rst +++ b/form/validation_group_service_resolver.rst @@ -14,8 +14,8 @@ parameter:: class ValidationGroupResolver { public function __construct( - private $service1, - private $service2, + private object $service1, + private object $service2, ) { } diff --git a/quick_tour/flex_recipes.rst b/quick_tour/flex_recipes.rst index 7829304e995..a71961d78af 100644 --- a/quick_tour/flex_recipes.rst +++ b/quick_tour/flex_recipes.rst @@ -200,13 +200,13 @@ rich API for a ``product`` table? Create a ``Product`` entity and give it the #[ORM\Id] #[ORM\GeneratedValue(strategy: 'AUTO')] #[ORM\Column(type: 'integer')] - private $id; + private int $id; #[ORM\Column(type: 'string')] - private $name; + private string $name; #[ORM\Column(type: 'integer')] - private $price; + private int $price; // ... } diff --git a/reference/constraints/Callback.rst b/reference/constraints/Callback.rst index 62184f805cd..2a14eff4531 100644 --- a/reference/constraints/Callback.rst +++ b/reference/constraints/Callback.rst @@ -99,7 +99,7 @@ field those errors should be attributed:: class Author { // ... - private $firstName; + private string $firstName; public function validate(ExecutionContextInterface $context, $payload) { diff --git a/reference/constraints/Expression.rst b/reference/constraints/Expression.rst index a51002040c0..26192069a8c 100644 --- a/reference/constraints/Expression.rst +++ b/reference/constraints/Expression.rst @@ -26,9 +26,9 @@ properties:: class BlogPost { - private $category; + private string $category; - private $isTechnicalPost; + private bool $isTechnicalPost; // ... @@ -155,7 +155,7 @@ assert that the expression must return ``true`` for validation to fail. "this.getCategory() in ['php', 'symfony'] or value == false", message: 'If this is a tech post, the category should be either php or symfony!', )] - private $isTechnicalPost; + private bool $isTechnicalPost; // ... } @@ -298,7 +298,7 @@ type (numeric, boolean, strings, null, etc.) 'value + error_margin < threshold', values: ['error_margin' => 0.25, 'threshold' => 1.5], )] - private $metric; + private float $metric; // ... } diff --git a/reference/constraints/Json.rst b/reference/constraints/Json.rst index 1ab6ce46494..04cd4d1c1e1 100644 --- a/reference/constraints/Json.rst +++ b/reference/constraints/Json.rst @@ -28,7 +28,7 @@ The ``Json`` constraint can be applied to a property or a "getter" method: #[Assert\Json( message: "You've entered an invalid Json." )] - private $chapters; + private string $chapters; } .. code-block:: yaml diff --git a/routing/custom_route_loader.rst b/routing/custom_route_loader.rst index 5caf51e8213..ec3b5efcb1b 100644 --- a/routing/custom_route_loader.rst +++ b/routing/custom_route_loader.rst @@ -278,7 +278,7 @@ you do. The resource name itself is not actually used in the example:: class ExtraLoader extends Loader { - private $isLoaded = false; + private bool $isLoaded = false; public function load($resource, string $type = null) { diff --git a/service_container/autowiring.rst b/service_container/autowiring.rst index 4f62e24e10f..72bb7851965 100644 --- a/service_container/autowiring.rst +++ b/service_container/autowiring.rst @@ -653,7 +653,7 @@ to inject the ``logger`` service, and decide to use setter-injection: class Rot13Transformer { - private $logger; + private LoggerInterface $logger; #[Required] public function setLogger(LoggerInterface $logger): void diff --git a/service_container/calls.rst b/service_container/calls.rst index 9f4e3699b18..cb364b59489 100644 --- a/service_container/calls.rst +++ b/service_container/calls.rst @@ -17,7 +17,7 @@ example:: class MessageGenerator { - private $logger; + private LoggerInterface $logger; public function setLogger(LoggerInterface $logger): void { @@ -84,7 +84,7 @@ instead of mutating the object they were called on:: class MessageGenerator { - private $logger; + private LoggerInterface $logger; public function withLogger(LoggerInterface $logger): self { diff --git a/service_container/configurators.rst b/service_container/configurators.rst index 2cf9cdb471f..7817a383761 100644 --- a/service_container/configurators.rst +++ b/service_container/configurators.rst @@ -23,7 +23,7 @@ You start defining a ``NewsletterManager`` class like this:: class NewsletterManager implements EmailFormatterAwareInterface { - private $enabledFormatters; + private array $enabledFormatters; public function setEnabledFormatters(array $enabledFormatters): void { @@ -40,7 +40,7 @@ and also a ``GreetingCardManager`` class:: class GreetingCardManager implements EmailFormatterAwareInterface { - private $enabledFormatters; + private array $enabledFormatters; public function setEnabledFormatters(array $enabledFormatters): void { diff --git a/service_container/injection_types.rst b/service_container/injection_types.rst index f1a6c80397b..5fb3d09a9c5 100644 --- a/service_container/injection_types.rst +++ b/service_container/injection_types.rst @@ -115,7 +115,7 @@ by cloning the original service, this approach allows you to make a service immu class NewsletterManager { - private $mailer; + private MailerInterface $mailer; /** * @return static @@ -222,7 +222,7 @@ that accepts the dependency:: // ... class NewsletterManager { - private $mailer; + private MailerInterface $mailer; #[Required] public function setMailer(MailerInterface $mailer): void diff --git a/service_container/service_decoration.rst b/service_container/service_decoration.rst index b772fbd2ad3..73b83271025 100644 --- a/service_container/service_decoration.rst +++ b/service_container/service_decoration.rst @@ -154,7 +154,7 @@ automatically changed to ``'.inner'``): { public function __construct( #[MapDecorated] - private $inner, + private object $inner, ) { } diff --git a/service_container/tags.rst b/service_container/tags.rst index 54c5c3cb461..31fe56cacc6 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -229,12 +229,7 @@ To begin with, define the ``TransportChain`` class:: class TransportChain { - private $transports; - - public function __construct() - { - $this->transports = []; - } + private array $transports = []; public function addTransport(\MailerTransport $transport): void { @@ -423,12 +418,7 @@ To begin with, change the ``TransportChain`` class:: class TransportChain { - private $transports; - - public function __construct() - { - $this->transports = []; - } + private array $transports = []; public function addTransport(\MailerTransport $transport, $alias): void { diff --git a/testing/database.rst b/testing/database.rst index fb78d33a3a7..bce96353da8 100644 --- a/testing/database.rst +++ b/testing/database.rst @@ -98,10 +98,7 @@ so, get the entity manager via the service container as follows:: class ProductRepositoryTest extends KernelTestCase { - /** - * @var \Doctrine\ORM\EntityManager - */ - private $entityManager; + private \Doctrine\ORM\EntityManager $entityManager; protected function setUp(): void { diff --git a/validation/groups.rst b/validation/groups.rst index 51981375e0e..22af24473be 100644 --- a/validation/groups.rst +++ b/validation/groups.rst @@ -23,14 +23,14 @@ user registers and when a user updates their contact information later: class User implements UserInterface { #[Assert\Email(groups: ['registration'])] - private $email; + private string $email; #[Assert\NotBlank(groups: ['registration'])] #[Assert\Length(min: 7, groups: ['registration'])] - private $password; + private string $password; #[Assert\Length(min: 2)] - private $city; + private string $city; } .. code-block:: yaml diff --git a/validation/sequence_provider.rst b/validation/sequence_provider.rst index 4ceb289a615..d409b1a203a 100644 --- a/validation/sequence_provider.rst +++ b/validation/sequence_provider.rst @@ -23,10 +23,10 @@ username and the password are different only if all other validation passes class User implements UserInterface { #[Assert\NotBlank] - private $username; + private string $username; #[Assert\NotBlank] - private $password; + private string $password; #[Assert\IsTrue( message: 'The password cannot match your username', @@ -180,13 +180,13 @@ entity and a new constraint group called ``Premium``: class User { #[Assert\NotBlank] - private $name; + private string $name; #[Assert\CardScheme( schemes: [Assert\CardScheme::VISA], groups: ['Premium'], )] - private $creditCard; + private string $creditCard; // ... } @@ -241,8 +241,8 @@ entity and a new constraint group called ``Premium``: class User { - private $name; - private $creditCard; + private string $name; + private string $creditCard; // ...