Skip to content
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

remove optional parameter value #582

Merged
merged 1 commit into from
Feb 9, 2022

Conversation

saeideng
Copy link
Collaborator

@saeideng saeideng commented Jan 31, 2022

I changed also the interface
not sure if it is BC
Closes #581

@codecov
Copy link

codecov bot commented Jan 31, 2022

Codecov Report

Merging #582 (99e2f5a) into master (cf3bff0) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff            @@
##             master     #582   +/-   ##
=========================================
  Coverage     95.00%   95.00%           
  Complexity      110      110           
=========================================
  Files            11       11           
  Lines           280      280           
=========================================
  Hits            266      266           
  Misses           14       14           
Impacted Files Coverage Δ
src/File/Writer/DefaultWriter.php 100.00% <ø> (ø)
src/Model/Behavior/UploadBehavior.php 94.73% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cf3bff0...99e2f5a. Read the comment docs.

@davidyell
Copy link
Member

Seems sensible, although with it typed as ?UploadedFileInterface will the default type be null ?

@saeideng
Copy link
Collaborator Author

saeideng commented Feb 1, 2022

Seems sensible, although with it typed as ?UploadedFileInterface will the default type be null ?

no default value , because you have to pass all parameters
(except when using named Argument)

@saeideng
Copy link
Collaborator Author

saeideng commented Feb 1, 2022

test with named Argument:

class A
{
    public string $first;
    public ?string $nullableString;
    public int $third;
    public function __construct(
        string $first,
        ?string $nullableString = null,
        int $third,
    ) {
       
        $this->first = $first;
        $this->nullableString = $nullableString;
        $this->third = $third;
    }
    public function dump(){
        var_dump($this->first);
        var_dump($this->nullableString);
        var_dump($this->third);
    }
}
$data = new A(
    'a string',
    third: 20,
    
);
$data->dump();

current implementation only works in php 8.0 when you use named Argument

//result php 8.0
string(8) "a string"
NULL
int(20)
//result php 8.1
Deprecated: Optional parameter $nullableString declared before required parameter $third is implicitly treated as a required parameter
Fatal error: Uncaught ArgumentCountError: A::__construct(): Argument #2 ($nullableString) not passed

@davidyell davidyell self-requested a review February 4, 2022 10:58
@saeideng
Copy link
Collaborator Author

saeideng commented Feb 8, 2022

extending class test:

class A
{
    public  $p;
    public function func(
        int $p = null
    ){
        $this->p = $p;
    }
}
class B extends A
{
    public  $p;
    public function func(
        ?int $p = null
    ) {
        $this->p = $p;
    }
    public function dump(){
        var_dump($this->p);
    }
}
$data = new B();
$data->func(50);
$data->dump();

php 7.1+

int(50)

another test:
implement interface ( testing __construct())

interface AInterface
{
    public function __construct(
        string $p = null
    );
}
class A implements AInterface
{
    public $p;
    public function __construct(
        ?string $p = null
    ) {
        $this->p = $p;
    }
    public function dump(){
        var_dump($this->p);
    }
}
$data = new A(
    'a string'
);
$data->dump();

php 7.1+

string(8) "a string"

@saeideng saeideng merged commit a66f7b9 into FriendsOfCake:master Feb 9, 2022
@saeideng saeideng deleted the optional-parameter branch February 9, 2022 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deprecated
3 participants