akalongman /
laravel-platfourm
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /* |
||
| 3 | * This file is part of the Laravel Platfourm package. |
||
| 4 | * |
||
| 5 | * (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Longman\Platfourm\Foundation\Exceptions; |
||
| 12 | |||
| 13 | use App; |
||
| 14 | use Exception; |
||
| 15 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||
| 16 | use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; |
||
| 17 | use Illuminate\Foundation\Validation\ValidationException; |
||
| 18 | use Slack; |
||
| 19 | use Symfony\Component\HttpKernel\Exception\HttpException; |
||
| 20 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
| 21 | |||
| 22 | class Handler extends ExceptionHandler |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * A list of the exception types that should not be reported. |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $dontReport = [ |
||
| 30 | AuthorizationException::class, |
||
| 31 | HttpException::class, |
||
| 32 | ModelNotFoundException::class, |
||
| 33 | ValidationException::class, |
||
| 34 | ]; |
||
| 35 | |||
| 36 | public function sendToSlack(Exception $e) |
||
| 37 | { |
||
| 38 | if (!App::environment('production')) { |
||
| 39 | return false; |
||
| 40 | } |
||
| 41 | |||
| 42 | $endpoint = config('slack.endpoint'); |
||
| 43 | if (strlen($endpoint) < 40) { |
||
| 44 | return false; |
||
| 45 | } |
||
| 46 | |||
| 47 | Slack::send($e->getMessage()); |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Render the given HttpException. |
||
| 52 | * |
||
| 53 | * @param \Symfony\Component\HttpKernel\Exception\HttpException $e |
||
| 54 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 55 | */ |
||
| 56 | protected function renderHttpException(HttpException $e) |
||
| 57 | { |
||
| 58 | $status = $e->getStatusCode(); |
||
| 59 | |||
| 60 | if (view()->exists("common.errors.{$status}")) { |
||
|
0 ignored issues
–
show
|
|||
| 61 | return response()->view("common.errors.{$status}", ['exception' => $e], $status, $e->getHeaders()); |
||
| 62 | } else { |
||
| 63 | return $this->convertExceptionToResponse($e); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Render an exception into an HTTP response. |
||
| 69 | * |
||
| 70 | * @param \Illuminate\Http\Request $request |
||
| 71 | * @param \Exception $e |
||
| 72 | * @return \Illuminate\Http\Response |
||
| 73 | */ |
||
| 74 | public function render($request, Exception $e) |
||
| 75 | { |
||
| 76 | if ($e instanceof ModelNotFoundException) { |
||
| 77 | $e = new NotFoundHttpException($e->getMessage(), $e); |
||
| 78 | } |
||
| 79 | |||
| 80 | if ($this->shouldRenderAsJson($request, $e)) { |
||
| 81 | return $this->renderAsJson($request, $e); |
||
|
0 ignored issues
–
show
The return type of
return $this->renderAsJson($request, $e); (Illuminate\Http\JsonResponse) is incompatible with the return type documented by Longman\Platfourm\Founda...eptions\Handler::render of type Illuminate\Http\Response.
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design. Let’s take a look at an example: class Author {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
abstract class Post {
public function getAuthor() {
return 'Johannes';
}
}
class BlogPost extends Post {
public function getAuthor() {
return new Author('Johannes');
}
}
class ForumPost extends Post { /* ... */ }
function my_function(Post $post) {
echo strtoupper($post->getAuthor());
}
Our function Loading history...
|
|||
| 82 | } |
||
| 83 | |||
| 84 | return parent::render($request, $e); |
||
| 85 | } |
||
| 86 | |||
| 87 | protected function shouldRenderAsJson($request, Exception $e) |
||
| 88 | { |
||
| 89 | if ($e instanceof ValidationException) { |
||
|
0 ignored issues
–
show
The class
Illuminate\Foundation\Va...ion\ValidationException does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed. Loading history...
|
|||
| 90 | return false; |
||
| 91 | } |
||
| 92 | |||
| 93 | return $request->wantsJson(); |
||
| 94 | } |
||
| 95 | |||
| 96 | protected function renderAsJson($request, Exception $e) |
||
|
0 ignored issues
–
show
|
|||
| 97 | { |
||
| 98 | // Define the response |
||
| 99 | $response = [ |
||
| 100 | 'errors' => 'Sorry, something went wrong.' |
||
| 101 | ]; |
||
| 102 | |||
| 103 | // If the app is in debug mode |
||
| 104 | if (config('app.debug')) { |
||
| 105 | // Add the exception class name, message and stack trace to response |
||
| 106 | $response['exception'] = (new \ReflectionClass($e))->getName(); |
||
|
0 ignored issues
–
show
Consider using
(new \ReflectionClass($e))->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
|
|||
| 107 | $response['message'] = $e->getMessage(); |
||
| 108 | $response['trace'] = $e->getTrace(); |
||
| 109 | } |
||
| 110 | |||
| 111 | // Default response of 400 |
||
| 112 | $status = 400; |
||
| 113 | |||
| 114 | // If this exception is an instance of HttpException |
||
| 115 | if ($this->isHttpException($e)) { |
||
| 116 | // Grab the HTTP status code from the Exception |
||
| 117 | $status = $e->getStatusCode(); |
||
|
0 ignored issues
–
show
It seems like you code against a specific sub-type and not the parent class
Exception as the method getStatusCode() does only exist in the following sub-classes of Exception: Illuminate\Foundation\Ht...aintenanceModeException, Longman\Platfourm\Auth\E...ions\ForbiddenException, Longman\Platfourm\Auth\E...s\UnauthorizedException, Longman\Platfourm\Founda...ions\ForbiddenException, Longman\Platfourm\Founda...s\InvalidValueException, Longman\Platfourm\Founda...s\UnauthorizedException, Longman\Platfourm\Founda...\ValueNotFoundException, Symfony\Component\HttpKe...cessDeniedHttpException, Symfony\Component\HttpKe...BadRequestHttpException, Symfony\Component\HttpKe...n\ConflictHttpException, Symfony\Component\HttpKe...ption\GoneHttpException, Symfony\Component\HttpKe...Exception\HttpException, Symfony\Component\HttpKe...thRequiredHttpException, Symfony\Component\HttpKe...NotAllowedHttpException, Symfony\Component\HttpKe...AcceptableHttpException, Symfony\Component\HttpKe...n\NotFoundHttpException, Symfony\Component\HttpKe...tionFailedHttpException, Symfony\Component\HttpKe...onRequiredHttpException, Symfony\Component\HttpKe...navailableHttpException, Symfony\Component\HttpKe...nyRequestsHttpException, Symfony\Component\HttpKe...authorizedHttpException, Symfony\Component\HttpKe...ableEntityHttpException, Symfony\Component\HttpKe...dMediaTypeHttpException. Maybe you want to instanceof check for one of these explicitly?
Let’s take a look at an example: abstract class User
{
/** @return string */
abstract public function getPassword();
}
class MyUser extends User
{
public function getPassword()
{
// return something
}
public function getDisplayName()
{
// return some name.
}
}
class AuthSystem
{
public function authenticate(User $user)
{
$this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
// do something.
}
}
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break. Available Fixes
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types
inside the if block in such a case.
Loading history...
|
|||
| 118 | } |
||
| 119 | |||
| 120 | // Return a JSON response with the response array and status code |
||
| 121 | return response()->json($response, $status); |
||
| 122 | } |
||
| 123 | |||
| 124 | } |
||
| 125 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: