for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of questocat/laravel-referral package.
*
* (c) questocat <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Questocat\Referral\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Cookie;
trait UserReferral
{
public function getReferralLink()
return url('/').'/?ref='.$this->affiliate_id;
affiliate_id
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
}
public static function scopeReferralExists(Builder $query, $referral)
return $query->whereAffiliateId($referral)->exists();
protected static function boot()
parent::boot();
static::creating(function ($model) {
if ($referredBy = Cookie::get('referral')) {
$model->referred_by = $referredBy;
$model->affiliate_id = self::generateReferral();
});
protected static function generateReferral()
$length = config('referral.referral_length', 5);
do {
$referral = str_random($length);
} while (static::referralExists($referral));
referralExists()
Questocat\Referral\Traits\UserReferral
scopeReferralExists()
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.
return $referral;
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: