File: Analyzer.php

package info (click to toggle)
phpunit-cli-parser 5.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,404 kB
  • sloc: php: 10,936; xml: 61; makefile: 14
file content (68 lines) | stat: -rw-r--r-- 1,759 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

declare(strict_types=1);

/**
 * Copyright (c) 2018-2026 Andreas Möller
 *
 * For the full copyright and license information, please view
 * the LICENSE.md file that was distributed with this source code.
 *
 * @see https://github.com/ergebnis/phpstan-rules
 */

namespace Ergebnis\PHPStan\Rules;

use PhpParser\Node;

/**
 * @internal
 */
final class Analyzer
{
    public function hasNullDefaultValue(Node\Param $parameter): bool
    {
        if (!$parameter->default instanceof Node\Expr\ConstFetch) {
            return false;
        }

        return 'null' === $parameter->default->name->toLowerString();
    }

    /**
     * @param null|Node\ComplexType|Node\Identifier|Node\Name $typeDeclaration
     */
    public function isNullableTypeDeclaration($typeDeclaration): bool
    {
        if ($typeDeclaration instanceof Node\NullableType) {
            return true;
        }

        if ($typeDeclaration instanceof Node\UnionType) {
            foreach ($typeDeclaration->types as $type) {
                if (
                    $type instanceof Node\Identifier
                    && 'null' === $type->toLowerString()
                ) {
                    return true;
                }

                if (
                    $type instanceof Node\Name\FullyQualified
                    && $type->hasAttribute('originalName')
                ) {
                    $originalName = $type->getAttribute('originalName');

                    if (
                        $originalName instanceof Node\Name
                        && 'null' === $originalName->toLowerString()
                    ) {
                        return true;
                    }
                }
            }
        }

        return false;
    }
}