LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SleepingOwl\Admin\Form\Element; |
||
| 4 | |||
| 5 | use Closure; |
||
| 6 | use Illuminate\Contracts\Support\Htmlable; |
||
| 7 | use Illuminate\Contracts\View\View as ViewContract; |
||
| 8 | use Illuminate\Http\Request; |
||
| 9 | use SleepingOwl\Admin\Form\FormElement; |
||
| 10 | |||
| 11 | class Custom extends FormElement |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * @var string|Closure |
||
| 15 | */ |
||
| 16 | protected $display; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var Closure |
||
| 20 | */ |
||
| 21 | protected $callback; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Custom constructor. |
||
| 25 | * |
||
| 26 | * @param Closure|null $callback |
||
| 27 | */ |
||
| 28 | public function __construct(Closure $callback = null) |
||
| 29 | { |
||
| 30 | if (! is_null($callback)) { |
||
| 31 | $this->setCallback($callback); |
||
| 32 | } |
||
| 33 | |||
| 34 | parent::__construct(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return Closure|mixed|string |
||
| 39 | */ |
||
| 40 | public function getDisplay() |
||
| 41 | { |
||
| 42 | if (is_callable($this->display)) { |
||
| 43 | return call_user_func($this->display, $this->getModel()); |
||
| 44 | } |
||
| 45 | |||
| 46 | if ($this->display instanceof Htmlable) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 47 | return $this->display->toHtml(); |
||
| 48 | } |
||
| 49 | |||
| 50 | if ($this->display instanceof View) { |
||
|
0 ignored issues
–
show
|
|||
| 51 | return $this->display->with('model', $this->getModel())->render(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $this->display; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Closure|string|Htmlable|ViewContract $display |
||
| 59 | * |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function setDisplay($display) |
||
| 63 | { |
||
| 64 | $this->display = $display; |
||
|
0 ignored issues
–
show
It seems like
$display can also be of type Illuminate\Contracts\Support\Htmlable or Illuminate\Contracts\View\View. However, the property $display is declared as type Closure|string. Maybe add an additional type check?
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly. For example, imagine you have a variable Either this assignment is in error or a type check should be added for that assignment. class Id
{
public $id;
public function __construct($id)
{
$this->id = $id;
}
}
class Account
{
/** @var Id $id */
public $id;
}
$account_id = false;
if (starsAreRight()) {
$account_id = new Id(42);
}
$account = new Account();
if ($account instanceof Id)
{
$account->id = $account_id;
}
Loading history...
|
|||
| 65 | |||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return Closure |
||
| 71 | */ |
||
| 72 | public function getCallback() |
||
| 73 | { |
||
| 74 | return $this->callback; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Closure $callback |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function setCallback(Closure $callback) |
||
| 83 | { |
||
| 84 | $this->callback = $callback; |
||
| 85 | |||
| 86 | return $this; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return $this|self|mixed |
||
| 91 | */ |
||
| 92 | public function render() |
||
| 93 | { |
||
| 94 | return $this->getDisplay(); |
||
|
0 ignored issues
–
show
|
|||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param \Illuminate\Http\Request $request |
||
| 99 | * |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | public function save(Request $request) |
||
| 103 | { |
||
| 104 | $callback = $this->getCallback(); |
||
| 105 | |||
| 106 | if (is_callable($callback)) { |
||
| 107 | call_user_func_array($callback, [$this->getModel(), $request]); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 |