PHPStan Extension: Dynamic Return Types with Value Objects - static-analysis

Some libraries (like Doctrine) use simple stringable value-objects, like Func:
<?php
class Func {
protected $name;
protected $arguments;
public function __construct($name, $arguments) {
$this->name = $name;
$this->arguments = $arguments;
}
public function __toString() {
return $this->name . '(' . implode(', ', $this->arguments) . ')';
}
}
$min = new Func('MIN', ['my_field']);
echo $min; // MIN(my_field)
Would it be possible for PHPStan to use a Conditional Return Type for the __toString() method, something like:
/**
* #return ($this->value is literal-string ? literal-string : string)
*/
If not, maybe a Dynamic Return Type could work? I have used DynamicMethodReturnTypeExtension::​getTypeFromMethodCall() before, but I'm not sure if it can remember the values that have gone into the object, either during __construct() or via other methods.
This relates to the PHPStan Doctrine Extension, where my intention is for __toString() to return the literal-string type when all the inputs to the value-object are literal-string, otherwise return a string.

Related

Export Excel in Laravel

**I have a problem with passing parameters on export file, I want to filter the export according to date selected on page. Hope you help me with this issue. Thanks **
This is my ExportController, I request data from form to my controller to give the export collection a date.
namespace App\Http\Controllers;
use Illuminate\Http\Response;
use Illuminate\Http\Request;
use App\Exports\ExportAttendance;
use Maatwebsite\Excel\Facades\Excel;
class ExportController extends Controller
{
public function export(Request $request)
{
return Excel::download(new ExportAttendance($request->input('min'),$request->input('max')),'Attendance.xlsx');
}
}
This is my ExportAttendance.php, this is responsible for the exportation of collections. On the query function I want to filter the data according on the date requested on the controller. How could I passed a data from controller to my Export.php, I did used constructors but it always return errors.
public function headings():array{
return[
'Name',
'In (AM)',
'Out (AM)',
'In (PM)',
'Out (PM)',
'Meeting',
'Task',
'Note',
'Total Hours',
'Date'
];
}
public function query()
{
$start = "2021-06-14";
$end = "2021-06-14";
return Attendance::select('Name','InAm','OutAM','InPM','OutPM','Meeting','SpecialTask','Undertime','TotalHours','Date')->whereBetween('Date',[$start,$end]);
}
public function collection()
{
return Attendance::all();
}
public function map($attendance):array
{
return[
$attendance->Name,
$attendance->InAM,
$attendance->OutAM,
$attendance->InPM,
$attendance->OutPM,
$attendance->Meeting,
$attendance->SpecialTask,
$attendance->Undertime,
$attendance->TotalHours,
$attendance->Date,
];
}
Instead of passing the $request object to the export class. You can simply use the request helper method.
public function query()
{
return Attendance::select('Name','InAm','OutAM','InPM','OutPM','Meeting','SpecialTask','Undertime','TotalHours','Date')
->whereBetween('Date',[request('start'), request('end')]);
}
Passing parameters is supposed to work too. Pls let me know what errors you see so I can update my answer.
I used Constructor
public function query()
{
$start = "2021-06-14";
$end = "2021-06-14";
return Attendance::select('Name','InAm','OutAM','InPM','OutPM','Meeting','SpecialTask','Undertime','TotalHours','Date')->where('Date','=',$this->year);
}
And this is my controller,
public function export(Request $request)
{
ob_start();
$datestart = $request->input('min');
$datestart = ob_get_contents();
return Excel::download(new ExportAttendance($datestart),'Attendance.xlsx');
ob_end_flush();
}
Another problem arise, when I used ob_end_clean() all my variable values returned nulls.

Custom Eloquent relation which sometimes returns $this Model

I have a Model Text which has a 1-to-many-relation called pretext(), returning a 1-to-many-Relationshop to Text, like so:
class Text extends Model
{
public function pretext(){
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
public function derivates(){
return $this->hasMany('App\Models\Text', 'pretext_id');
}
}
If a $text does not have any pretext (which, in my scenario, means $text['pretext_id'] == 0) $text->pretext() shall return the $text itself. When I try
public function pretext(){
if ( $this->belongsTo('App\Models\Text', 'pretext_id') ) {
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
else {
return $this;
}
}
I get the error
local.ERROR: LogicException: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
when the else-part is executed. So my questions are:
How do I turn $this into an object of type Relation? or alternatively:
How can I achieve my goal on a different way?
Try dynamic props
class Text extends Model
{
protected $appends = ['content'];
public function pretext(){
return $this->belongsTo('App\Models\Text', 'pretext_id');
}
public function getContentAttribute(){
$pretext = $this->pretext()->get();
if ($pretext->count()) {
return $pretext;
}
return $this;
}
}
Then in controller or view if you have the instance
(consider optimizing it if you have N+1 issues)
$obj = Text::find(1);
dd($obj->content);
I think you can create another method that calling pretext() and check the returned value.
public function getPretext() {
$value = pretext();
return ($value)? $value : $this;
}

Laravel: override eloquent function for get results?

I can override function before save :
public function save(array $options = [])
{
if(isset($this->datesConvert)){
foreach($this->datesConvert as $date){
$this->attributes[$date] = Carbon::createFromFormat('d/m/Y', $this->attributes[$date])->format('Y-m-d');
}
}
parent::save($options);
}
But which method I can use for get result ? and where is documentation for this. I am looking for something like :
public function get()
{
parent::get();
if(isset($this->datesConvert)){
foreach($this->datesConvert as $date){
$this->attributes[$date] = Carbon::createFromFormat('Y-m-d', $this->attributes[$date])->format('d/m/Y');
}
}
}
With that I can convert 10 date rows without need of 20 mutators..
It seems that Attribute casting fits your needs or use Date mutators
You may customize which fields are automatically mutated, and even completely disable this mutation, by overriding the $dates property of your model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = [
'created_at',
'updated_at',
'deleted_at',
// more dates
];
}
EDIT
Another way, you can override getAttribute method in Model
<?php
namespace App;
use Carbon\Carbon;
trait DateFormatting
{
protected function dateFields()
{
return [];
}
public function getAttribute($key)
{
if ( array_key_exists( $key, $this->dateFields() ) ) {
return Carbon::createFromFormat('d/m/Y', $this->attributes[$key])->format('Y-m-d');
}
return parent::getAttribute($key);
}
}
then you can use this trait in any your model, just don't forget override dateFields in it
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\DateFormatting;
class User extends Model
{
use DateFormatting;
protected function dateFields()
{
return [
'finished_at',
// other field names that you need to format
];
}
after all you can access to this fields as usual(using magic __get())
$model->finished_at;
I find a solution, My solution is :
public function save(array $options = [])
{
if(isset($this->datesConvert)){
foreach($this->datesConvert as $date){
$this->attributes[$date] = \Carbon\Carbon::createFromFormat('d/m/Y', $this->attributes[$date])->format('Y-m-d');
}
}
parent::save($options);
}
public function getAttribute($key)
{
$value = parent::getAttribute($key);
if(isset($this->attributes[$key])){
if(isset($this->datesConvert) && in_array($key, $this->datesConvert)){
$value = \Carbon\Carbon::createFromFormat('Y-m-d', $value)->format('d/m/Y');
}
}
return $value;
}
Laravel comes with something very useful for this problem. I'm not sure what it's called, but you can modify attributes or even add new attributes like this:
class YourModel extends Model
{
...
public function getDateAttribute()
{
return Carbon::createFromFormat('Y-m-d', $this->attributes[$date])->format('d/m/Y');
}
...
}
You can retrieve this attribute like:
$yourModel->date;
Edit after comment #fico7489
You can't ignore the fact you always have to modify things. However there are still some solutions to make it easier.
For example you can change your date column to a string and just store your date with the desired date format.
Other solution should be modifying the date through sql. FORMAT(Now(),'YYYY-MM-DD').
Example in laravel would look like (not tested):
YourModel::select([
'*',
DB::raw('
FORMAT(yourDateColumn,'YYYY-MM-DD')
')
])->get();

How to set multiple boolean mutators

I'm using Laravel 4 and I have a model with a lot of boolean attributes.
For each of them I'm setting a setter like this
public function setIsRemoteAttribute($value){
$this->attributes['isRemote'] = !!$value;
}
and a getter like this
public function getIsRemoteAttribute($value){
return !! $this->attributes['isRemote'];
}
Is there any way to abstract that out so I'm not individually setting 12+ mutators?
I guess you can override setAttribute method like:
public function setAttribute($key, $value){
if(in_array($key, 'abstract_keys')){
$this->attributes[$key] = !!$value;
}
else{
parent::setAttribute($key, $value);
}
}
Same would go for getAttribute.
I have L5 installation but I'm pretty sure this will apply to L4.2 as well.
If you look in the code for Eloquent's Model class you will find the following method:
/**
* Set a given attribute on the model.
*
* #param string $key
* #param mixed $value
* #return void
*/
public function setAttribute($key, $value)
{
// First we will check for the presence of a mutator for the set operation
// which simply lets the developers tweak the attribute as it is set on
// the model, such as "json_encoding" an listing of data for storage.
if ($this->hasSetMutator($key))
{
$method = 'set'.studly_case($key).'Attribute';
return $this->{$method}($value);
}
// If an attribute is listed as a "date", we'll convert it from a DateTime
// instance into a form proper for storage on the database tables using
// the connection grammar's date format. We will auto set the values.
elseif (in_array($key, $this->getDates()) && $value)
{
$value = $this->fromDateTime($value);
}
if ($this->isJsonCastable($key))
{
$value = json_encode($value);
}
$this->attributes[$key] = $value;
}
You could potentially, override this function in your own model:
Store a list of attributes that should get the boolean mutator
Check if $key is within this list of elements
If it is - do something
If it's not, default to the parent implementation (This method)
Example:
public function setAttribute($key, $value)
{
if (in_array($key, $this->booleans))
{
// Do your stuff here - make sure to return it
}
return parent::setAttribute($key, $value);
}
You can do the same thing for the getAttribute method.
With this approach, all you need to do is add the names of the attributes to the list of booleans for them to work.
protected $booleans = array('attr1', 'attr2');

Laravel static call of Overrided Eloquent's first() method

if I override eloquent's first() method I can not call the method statically (Through facade) as I would expect. I would expect that implemented __callStatic() method will be used (implemented in Illuminate\Database\Eloquent\Model), but that's not the case.
So I tried to implement the magic method myself. I still cannot access overrided first() method statically.
ErrorException: Non-static method Entity::first() should not be
called statically, assuming $this from incompatible context.
class Entity extends Eloquent
{
public function first($columns = ['*'])
{
if (Cache::tags(static::getTags())->has('first')) :
return Cache::tags(static::getTags())->get('first');
endif;
$result = parent::first($columns);
if ($result) :
Cache::tags(static::getTags())->put('first', $result, with(new static)->ttl);
endif;
return $result;
}
public static function __callStatic($method, $parameters)
{
$instance = new static;
return call_user_func_array(array($instance, $method), $parameters);
}
}
What I'm missing here?
I've expressed my doubts about the necessity to override first() for caching purposes, but of course it is technically possible to do it.
You have to create your own Builder class for that:
class MyBuilder extends Illuminate\Database\Eloquent\Builder {
/**
* Execute the query and get the first result.
*
* #param array $columns
* #return \Illuminate\Database\Eloquent\Model|static|null
*/
public function first($columns = array('*'))
{
if ($someCondition) :
return 1;
endif;
return parent::first($columns);
}
}
And then make your model use this builder by implementing the newEloquentBuilder method:
public function newEloquentBuilder($query)
{
return new MyBuilder($query);
}

Resources