yiisoft / yiisoft/definitions

Optimizing constructor and argument initialization

Open
#91 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

status:under discussion
Dominant language
PHP
Stars
15
Forks
6
Avg merge
1h 1m
Merged PRs (30d)
1

Description

I propose to add an alternative syntax for a simplified notation of the constructor and its arguments.
For example:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    'class' => MyServiceInterface::class,
    '__construct()' => [42], 
    '$propertyName' => 'value',
    'setName()' => ['Alex'],
]);

So it can be written like this:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    MyServiceInterface::class,
    [42], 
    'propertyName' => 'value',
    'setName()' => ['Alex'],
]);

Here the unnamed first (0) parameter (string) is the name of the class.
The second unnamed (1) parameter (array, if present) is the constructor arguments.

Raise an error if unnamed parameters are present along with "__construct()" or "class".

Also, the '$' notation here may be redundant and looks like a developer error visually (lack of double quotes).

This improvement will simplify development and reduce the amount of code.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names ArrayDefinition::fromConfig as the entry point; start there and inspect how class, constructor, property, and setter definitions are currently handled. Define and test the shorthand, including errors when unnamed parameters are mixed with "__construct()" or "class", and resolve whether the "$" property notation should remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.