Check for mismatching type of a variable passed in as a parameter
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Spatie\BladeX; |
||
4 | |||
5 | use Closure; |
||
6 | use Illuminate\Contracts\Support\Arrayable; |
||
7 | use Spatie\BladeX\Exceptions\CouldNotRegisterBladeXComponent; |
||
8 | |||
9 | class BladeXComponent |
||
10 | { |
||
11 | /** @var string */ |
||
12 | public $bladeViewName; |
||
13 | |||
14 | /** @var string */ |
||
15 | public $name; |
||
16 | |||
17 | /** @var string */ |
||
18 | public $viewModel; |
||
19 | |||
20 | public static function make(string $bladeViewName, string $name = '') |
||
21 | { |
||
22 | return new BladeXComponent($bladeViewName, $name = ''); |
||
23 | } |
||
24 | |||
25 | public function __construct(string $bladeViewName, string $name = '') |
||
26 | { |
||
27 | $bladeViewName = str_replace('.', '/', $bladeViewName); |
||
28 | |||
29 | if ($name === '') { |
||
30 | $baseComponentName = explode('/', $bladeViewName); |
||
31 | |||
32 | $name = kebab_case(end($baseComponentName)); |
||
33 | } |
||
34 | |||
35 | if (! view()->exists($bladeViewName)) { |
||
36 | throw CouldNotRegisterBladeXComponent::viewNotFound($bladeViewName, $name); |
||
37 | } |
||
38 | |||
39 | $this->bladeViewName = $bladeViewName; |
||
40 | |||
41 | $this->name = $name; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string|\Closure $viewModel |
||
46 | * |
||
47 | * @return $this |
||
48 | */ |
||
49 | public function viewModel($viewModel) |
||
50 | { |
||
51 | if (is_callable($viewModel)) { |
||
52 | $this->viewModel = $this->createClosureViewModel($viewModel); |
||
0 ignored issues
–
show
|
|||
53 | |||
54 | return $this; |
||
55 | } |
||
56 | |||
57 | if (! class_exists($viewModel)) { |
||
58 | throw CouldNotRegisterBladeXComponent::viewModelNotFound($this->name, $viewModel); |
||
59 | } |
||
60 | |||
61 | if (! is_a($viewModel, Arrayable::class, true)) { |
||
62 | throw CouldNotRegisterBladeXComponent::viewModelNotArrayable($this->name, $viewModel); |
||
63 | } |
||
64 | |||
65 | $this->viewModel = $viewModel; |
||
66 | |||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | protected function createClosureViewModel(Closure $closure): string |
||
71 | { |
||
72 | static $viewModelCounter = 0; |
||
73 | |||
74 | $viewModelClassName = 'bladex.viewModel.'.$viewModelCounter++; |
||
75 | |||
76 | app()->bind($viewModelClassName, function ($app, $arguments) use ($closure) { |
||
77 | return (new ClosureViewModel($arguments ?? [])) |
||
78 | ->withClosure($closure); |
||
79 | }); |
||
80 | |||
81 | return $viewModelClassName; |
||
82 | } |
||
83 | } |
||
84 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.