1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\SDL; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Exception\NotReadableException; |
13
|
|
|
use Railt\Io\File; |
14
|
|
|
use Railt\Reflection\Contracts\Document; |
15
|
|
|
use Railt\SDL\Exception\CompilerException; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
if (! \function_exists('sdl')) { |
19
|
|
|
/** |
20
|
|
|
* @param string $fileOrSources |
21
|
|
|
* @param bool $filename |
22
|
|
|
* @return Document |
23
|
|
|
* @throws CompilerException |
24
|
|
|
* @throws NotReadableException |
25
|
|
|
*/ |
26
|
|
|
function sdl(string $fileOrSources, bool $filename = false): Document |
27
|
|
|
{ |
28
|
|
|
$file = $filename ? File::fromPathname($fileOrSources) : File::fromSources($fileOrSources); |
29
|
|
|
|
30
|
|
|
return (new Compiler())->compile($file); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (! \function_exists('object_to_string')) { |
35
|
|
|
/** |
36
|
|
|
* @param object $object |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
function object_to_string($object): string |
40
|
|
|
{ |
41
|
1 |
|
\assert(\is_object($object)); |
42
|
|
|
|
43
|
1 |
|
$hash = \function_exists('\\spl_object_id') |
44
|
1 |
|
? \spl_object_id($object) |
45
|
1 |
|
: \spl_object_hash($object); |
46
|
|
|
|
47
|
1 |
|
if (is_renderable($object)) { |
48
|
|
|
return \sprintf('%s(%s)#%s', \get_class($object), (string)$object, $hash); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return \sprintf('%s#%s', \get_class($object), $hash); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
if (! \function_exists('is_renderable')) { |
57
|
|
|
/** |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
function is_renderable($value): bool |
62
|
|
|
{ |
63
|
1 |
|
return \is_scalar($value) || (\is_object($value) && \method_exists($value, '__toString')); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.