main Flashcards
Laravel Cashier
Laravel’s package for fully integrating Stripe or other payment methods with minimum effort
Multiple inheritance in PHP
There is no multiple inheritance in PHP, but there are better methods. Favor composition over inheritance, use traits, interfaces…
Final method
A child extending the parent can’t override the method declared as final
Final class
A final class is not able to be inherited / extended
When to use final
Make your classes always final, if they implement an interface, and no other public methods are defined
- Preventing massive inheritance chain of doom
- Encouraging composition
- Force the developer to think about user public API
- Force the developer to shrink an object’s public API
- A final class can always be made extensible
- extends breaks encapsulation
- You don’t need that flexibility
- You are free to change the code
When to avoid Final
When to avoid final:
Final classes only work effectively under following assumptions:
- There is an abstraction (interface) that the final class implements
- All of the public API of the final class is part of that interface
If one of these two pre-conditions is missing, then you will likely reach a point in time when you will make the class extensible, as your code is not truly relying on abstractions.
Laravel Passport vs Laravel Sanctum
If your application absolutely needs to support OAuth2, then you should use Laravel Passport.
However, if you are attempting to authenticate a single-page application, mobile application, or issue API tokens, you should use Laravel Sanctum. Laravel Sanctum does not support OAuth2; however, it provides a much simpler API authentication development experience.
Laravel Sanctum problems it solves
API Tokens
First, Sanctum is a simple package you may use to issue API tokens to your users without the complication of OAuth. This feature is inspired by GitHub and other applications which issue “personal access tokens”. For example, imagine the “account settings” of your application has a screen where a user may generate an API token for their account. You may use Sanctum to generate and manage those tokens. These tokens typically have a very long expiration time (years), but may be manually revoked by the user at anytime.
Laravel Sanctum offers this feature by storing user API tokens in a single database table and authenticating incoming HTTP requests via the Authorization header which should contain a valid API token.
SPA Authentication
Second, Sanctum exists to offer a simple way to authenticate single page applications (SPAs) that need to communicate with a Laravel powered API. These SPAs might exist in the same repository as your Laravel application or might be an entirely separate repository, such as a SPA created using Vue CLI or a Next.js application.
For this feature, Sanctum does not use tokens of any kind. Instead, Sanctum uses Laravel’s built-in cookie based session authentication services. Typically, Sanctum utilizes Laravel’s web authentication guard to accomplish this. This provides the benefits of CSRF protection, session authentication, as well as protects against leakage of the authentication credentials via XSS.
Sanctum will only attempt to authenticate using cookies when the incoming request originates from your own SPA frontend. When Sanctum examines an incoming HTTP request, it will first check for an authentication cookie and, if none is present, Sanctum will then examine the Authorization header for a valid API token.
Service Providers
Service providers are the central place of all Laravel application bootstrapping. Your own application, as well as all of Laravel’s core services, are bootstrapped via service providers.
But, what do we mean by “bootstrapped”? In general, we mean registering things, including registering service container bindings, event listeners, middleware, and even routes. Service providers are the central place to configure your application.
If you open the config/app.php file included with Laravel, you will see a providers array. These are all of the service provider classes that will be loaded for your application. By default, a set of Laravel core service providers are listed in this array. These providers bootstrap the core Laravel components, such as the mailer, queue, cache, and others. Many of these providers are “deferred” providers, meaning they will not be loaded on every request, but only when the services they provide are actually needed.
Traits
It’s a piece of code that doesn’t need to be instantiated. It’s used in a model for instance, and it gives you the posibility to reuse code without repeating. This is code that is “pasted” into your model, it’s not being extended, this means that if you have a method “hello()” in your model and a method “hello()” in your trait, you’ll experience an error. You can bypass this error this way:
use TestTrait {
hello as traitHello
}
Que es Psalm y que significa (errorLevel=”3”)
Psalm: Static analisis tool made by Vimeo. It also has some detecting failures of security settings with a flag.
level 1 is the most strict, level 8 is the most lenient.
Que es Psalm y que significa (errorLevel=”3”)
Psalm: Static analisis tool made by Vimeo. It also has some detecting failures of security settings with a flag.
level 1 is the most strict, level 8 is the most lenient.
Open source
Que es PHPStan y que significa (level: 7)
PHP Static Analysis Tool. PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code. It moves PHP closer to compiled languages in the sense that the correctness of each line of the code can be checked before you run the actual line.
0 is the loosest, 9 is the strictest.
Open source
Que es Rector,
composer require rector/rector –dev
vendor/bin/rector init
vendor/bin/rector –dry-run
Rector is a CLI tool written in PHP. It can instantly upgrade old PHP code and handle automated refactorings. It’s fast and precise - changes 5000 files under minute.
Open source
Que es PHPCS,
PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.