PHP code example of rector / type-perfect

1. Go to this page and download the library: Download rector/type-perfect library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rector / type-perfect example snippets


private ?SomeType $someType = null;

if (! empty($this->someType)) {
    // ...
}

if (! isset($this->someType)) {
    // ...
}

// here we only know, that $this->someType is not empty/null

if (! $this->someType instanceof SomeType) {
    return;
}

// here we know $this->someType is exactly SomeType

$article = new Article();

$id = $article['id'];
// we have no idea, what the type is

$id = $article->getId();
// we know the type is int

public function getProduct()
{
    if (...) {
        return $product;
    }

    return false;
}

public function getProduct(): ?Product
{
    if (...) {
        return $product;
    }

    return null;
}

private $someType;

public function run()
{
    $this->someType->vale;
}

private SomeType $someType;

public function run()
{
    $this->someType->value;
}

private $someType;

public function run()
{
    $this->someType->someMetho(1, 2);
}

private SomeType $someType;

public function run()
{
    $this->someType->someMethod(1, 'active');
}

// in one file
$product->addPrice(100.52);

// another file
$product->addPrice(52.05);

final class ConferenceTalk extends Talk
{
    public function bookHotel()
    {
        // ...
    }
}

final class MeetupTalk extends Talk
{
    public function bookTrain()
    {
        // ...
    }
}

final class TalkFactory
{
    public function createConferenceTalk(): Talk
    {
        return new ConferenceTalk();
    }

    public function createMeetupTalk(): Talk
    {
        return new MeetupTalk();
    }
}

$talk instanceof ConferenceTalk
yaml
parameters:
    type_perfect:
        no_mixed_property: true
        no_mixed_caller: true
        null_over_false: true
        narrow_param: true
        narrow_return: true
diff

-/**
- * @param float $price
- */
-public function addPrice($price)
+public function addPrice(float $price)
{
    $this->price = $price;
}