Code Monkey home page Code Monkey logo

laravel-nullable's Introduction

Laravel Nullable

Do not let "null" to impersonate your objects.

StyleCI Quality Score Code Coverage Latest Stable Version PHP from Packagist License

Functional programming paradigm in laravel

Built with โค๏ธ for every smart laravel developer

Null is usually used to represent a missing value (for ex when we can't find a row with a partcular Id we return null) And that is the BAD IDEA, we are going to kill off !!!

๐Ÿ”ฅ Installation:

composer require imanghafoori/laravel-nullable

This package exposes a nullable() global helper function with which you can wrap variables which sometimes are object and sometimes null.

Consider this:

$email = TwitterApi::find(1)->email;

Now this code is working fine But...

What if the user with ID of 1 gets deleted in future ?!

null->email and crap ! ๐Ÿ˜ง

So if you forget to handle the null with an if statement, you will have errors.

You need something to FORCE you and the users of your class methods to handle the null cases.

To prevent such errors, you should code like this:

$user = $twitterApi->find($id);

if ($user === null) {
    return redirect()->route('page_not_found');
}

โ–ถ๏ธ Nullables to rescue !!!

To refactor the code above, first

You have to change your repo class :

// the old way:

/**
* @return User|null            <---- consider here. We are returning two types !!!
*/
public function find ($id) {
     $user = TwitterApi::search($id);
     
     if (!$user) {
         return null;
     }
     return new User($user);   
}

The above code returns 2 types, and That is the source of confusion for method callers. They get ready for one type, and forget about the other.

Let's do a small change to it:

/**
 * @return Nullable        <---- we now have only one consistent type. Not two.
 */
public function find ($id) {
     $user = TwitterApi::search($id);
     
     if (!$user) {
         return new Nullable(null);   //  <----  instead of pure null;
     }
     $user = new User($user);   
     
     $message = 'Model Not Found with Id : '. $id;

     return new Nullable($user, [$message]);   //  <----  instead of User;
}

๐Ÿ”” Now our method consistently returns Nullable objects, no matter what :)

After this change, no one can have access to the real meat of your repo (in this case User object) unless he/she gives a way to handle the null case. No if(is_null()) is required, No exception handling is required.

Remember PHP does not force us to write that if, and we as humen always tend to forget it.

And that makes a differnce ! Before it was easy to forget, but it is impossible to continue if you forget !!!

$userObj = $userRepo->find($id)->getOrSend(function ($message) {

  return redirect()->route('page_not_found')->with('error', $message);
});

// Call a static method.
$userObj = $twitterApi->find($id)->getOrSend([Response::class, 'pageNotFound']);

// or a get default value
$userObj = $twitterApi->find($id)->getOr(new User());

Now we are sure $user is not null and we can sleep better at night !

โ–ถ๏ธ Testing:

An other advantage is that, if you use nullable and you forget to write a test that simulates the situations where null values are returned, phpunit code coverage highlights the closure you have passed to the ->getOrDo() (or similar methods) as none-covered, indicating that there is a missing test.

but if you return the object directly, you can get 100% code coverage without having a test covering nully situations, hence hidden errors may still lurk you at 100% coverage.

โ–ถ๏ธ Q & A :

Why throwing exceptions is not always the best idea?!

When you throw an exception you should always ask your self. Is there any body out there to catch it ?? What if they forget to catch and handle the exception ?! It is the same issue as the null. It cases error.

The point is to give no way to continue, if they forget to handle the failures.

More from the author:

Laravel middlewarize (new*)

๐Ÿ’Ž You can put middleware on any method calls.


Laravel Hey Man

๐Ÿ’Ž It allows to write expressive code to authorize, validate and authenticate.


Laravel Terminator

๐Ÿ’Ž A minimal yet powerful package to give you opportunity to refactor your controllers.


Laravel AnyPass

๐Ÿ’Ž It allows you login with any password in local environment only.


Eloquent Relativity

๐Ÿ’Ž It allows you to decouple your eloquent models to reach a modular structure


laravel-nullable's People

Contributors

amirsadeghi1 avatar asedmammad avatar i-iman-i avatar imanghafoori1 avatar mostafanorzade avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-nullable's Issues

issue with laravel 8.1

composer require imanghafoori/laravel-nullable
Using version ^1.2 for imanghafoori/laravel-nullable
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for imanghafoori/laravel-nullable ^1.2 -> satisfiable by imanghafoori/laravel-nullable[1.2.4, v1.2.0, v1.2.1, v1.2.2, v1.2.3].
- Installation request for laravel/framework (locked at v8.1.0, required as ^8.0) -> satisfiable by laravel/framework[v8.1.0].
- Can only install one of: laravel/framework[8.x-dev, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.0.0, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.0.1, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.0.2, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.0.3, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.0.4, 5.7.x-dev].
- Can only install one of: laravel/framework[v8.1.0, 5.7.x-dev].
- Can only install one of: laravel/framework[5.7.x-dev, v8.1.0].
- Can only install one of: laravel/framework[5.7.x-dev, v8.1.0].
- Conclusion: install laravel/framework 5.7.x-dev
- Installation request for laravel/framework ^8.0 -> satisfiable by laravel/framework[8.x-dev, v8.0.0, v8.0.1, v8.0.2, v8.0.3, v8.0.4, v8.1.0].

Installation failed, reverting ./composer.json to its original content.

laravel and php version error

Your requirements could not be resolved to an installable set of packages.

Problem 1
- imanghafoori/laravel-nullable[v1.2.0, ..., v1.2.2] require laravel/framework ~5.1 -> found laravel/framework[v5.1.0, ..., 5.8.x-dev] but it conflicts with your root composer.json require (^7.0).
- imanghafoori/laravel-nullable v1.2.3 requires laravel/framework ~5.1|6.* -> found laravel/framework[v5.1.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev] but it conflicts with your root composer.json require (^7.0).
- imanghafoori/laravel-nullable[v1.2.4, ..., v1.2.5] require php ^7.1.3|7.2.|7.3.|7.4.* -> your php version (8.0.9) does not satisfy that requirement.
- Root composer.json requires imanghafoori/laravel-nullable ^1.2 -> satisfiable by imanghafoori/laravel-nullable[v1.2.0, ..., v1.2.5].

Use the option --with-all-dependencies (-W) to allow upgrades, downgrades and removals for packages currently locked to specific versions.

Possible issue with Laravel 11 on PHP 8.3.*?

Hi Iman,

We're big fans of your Laravel packages at our company and greatly appreciate all your open source efforts and support!

We were exploring whether we could upgrade to Laravel 11 recently (on php 8.3.4) and we hit a few errors that seem related to to laravel-nullable (as a dependency of us directly using laravel-password-history), but we can't figure out why it's not picking up the versioning correctly (see errors below). I confirmed that the composer.json file for laravel-nullable has php 8.3.* and laravel 11.*. We feel like maybe we're missing the right version operator or something similar. We know you're busy, but wondered if there was something we're missing that you could identify when you have a moment to take a look. Let me know if any other information would help. Thanks so much for any ideas!

Error output from composer update:

    - imanghafoori/laravel-nullable[v1.2.0, ..., v1.2.2] require laravel/framework ~5.1 -> found laravel/framework[v5.1.0, ..., v5.8.38] but it conflicts with your root composer.json require (^11.0).
    - imanghafoori/laravel-nullable v1.2.3 requires laravel/framework ~5.1|6.* -> found laravel/framework[v5.1.0, ..., v5.8.38, v6.0.0, ..., v6.20.44] but it conflicts with your root composer.json require (^11.0).
    - imanghafoori/laravel-nullable[v1.2.4, ..., v1.2.5] require php ^7.1.3|7.2.*|7.3.*|7.4.* -> your php version (8.3.4) does not satisfy that requirement.
    - imanghafoori/laravel-nullable v1.2.6 requires php ^7.1.3|7.2.*|7.3.*|7.4.*|8.0.* -> your php version (8.3.4) does not satisfy that requirement.
    - imanghafoori/laravel-nullable v1.2.7 requires php ^7.1.3|7.2.*|7.3.*|7.4.*|8.0.*|8.1.* -> your php version (8.3.4) does not satisfy that requirement.
    - imanghafoori/laravel-nullable v1.2.8 requires php ^7.1.3|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.* -> your php version (8.3.4) does not satisfy that requirement.
    - Root composer.json requires imanghafoori/laravel-password-history ^1.0.2 -> satisfiable by imanghafoori/laravel-password-history[v1.0.2].
    - imanghafoori/laravel-nullable v1.2.9 requires laravel/framework ~5.1|6.*|7.*|8.*|9.*|10.*|11.* -> satisfiable by laravel/framework[v11.0.0, ..., v11.0.7].
    - imanghafoori/laravel-password-history v1.0.2 requires imanghafoori/laravel-nullable ^1.2 -> satisfiable by imanghafoori/laravel-nullable[v1.2.0, ..., v1.2.9].

Related snippets from composer.lock:

            "name": "imanghafoori/laravel-password-history",
            "version": "v1.0.2",
            "source": {
                "type": "git",
                "url": "https://github.com/imanghafoori1/laravel-password-history.git",
                "reference": "e95c72dfa1c1e10b1bd9ace54bd868dea28e871f"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/imanghafoori1/laravel-password-history/zipball/e95c72dfa1c1e10b1bd9ace54bd868dea28e871f",
                "reference": "e95c72dfa1c1e10b1bd9ace54bd868dea28e871f",
                "shasum": ""
            },
            "require": {
                "imanghafoori/laravel-nullable": "^1.2",
                "imanghafoori/laravel-smart-facades": "^1.0",
                "laravel/framework": "~5.1|6.*|7.*|8.*|9.*|10.*|11.*",
                "php": "^7.1.3|7.2.*|7.3.*|7.4.*|8.*"
            },
            "name": "imanghafoori/laravel-nullable",
            "version": "v1.2.9",
            "source": {
                "type": "git",
                "url": "https://github.com/imanghafoori1/laravel-nullable.git",
                "reference": "ff47c2c273ac47a8a0d24eef1c3df2fabf45a52e"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/imanghafoori1/laravel-nullable/zipball/ff47c2c273ac47a8a0d24eef1c3df2fabf45a52e",
                "reference": "ff47c2c273ac47a8a0d24eef1c3df2fabf45a52e",
                "shasum": ""
            },
            "require": {
                "laravel/framework": "~5.1|6.*|7.*|8.*|9.*|10.*|11.*",
                "php": "^7.1.3|7.2.*|7.3.*|7.4.*|8.0.*|8.1.*|8.2.*|8.3.*"
            },

php version error

hello i got this problem when i try to install package
Problem 1
- This package requires php 7.1 but your PHP version (7.4.7) does not satisfy that requirement.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.