r/PHPhelp • • Feb 11 '21

Can someone code review my PHP project?

Introduction

I've been lucky enough to get my first job as a junior PHP developer. I am concerned I'm not good enough because I am only 16. I would like to brush up on my coding and get as much constructive criticism as I can.

What is it?

This is a SQL query builder, built for MySQL that provides a fluent, chaining API to build a query, and allows you to output the raw SQL. How can I improve the quality of it, as well as any features you can think of that I can try to practice.

API:

https://gist.github.com/WholePatient9125/bb51995a4e9d7fef8fab9184f663698b

Gist class (QueryBuilder): https://gist.github.com/WholePatient9125/cd8b275882836d49e30e1df55ffdebb7

Many, many thanks in advance to everyone who helps.

I know the support for multiple where clauses is weak, and maybe I forgot to add some options and be more flexible, but this was to the best of my basic SQL knowledge, I'm open to challenges.

9 Upvotes

12 comments sorted by

2

u/Xnuiem Feb 11 '21

I would expect it to be andWhere considering the other is orWhere.

Group? Order can have more one column and direction.

Looks clean enough. Comment would be good to make it self documenting.

1

u/Whole_Patience9125 Feb 11 '21

Hi! Thanks for your comments. One thing I did notice is I did add support for order by's direction (ascending or descending), AFAIK. I'll look into your other recommendations.

2

u/Bigdrums Feb 12 '21

This is specific to the query builder class. I think a query builder is a great learning project.

Query builders usually use a fluid interface to allow chaining of methods which is what you are doing here, but the table is set via the constructor. Sticking with this fluid design, I would recommend including a table setter method. I feel like $builder->table('users')->where('email','=', '...') is easier to understand at a quick glance. You will find this common with other query builders.

Another item I might look at is naming convention, specifically the use of the word 'join' in your where method. Join is used in SQL for joining tables and this can lead to some confusion for people reading the code, or if you decide to implement joins down the road. Perhaps something like 'delimiter' might work. Whatever it is, just not a name that already has meaning in the context of the package.

I noticed is there is no interaction with PDO or mysqli directly in the class but I am going to assume that the query being generated here is eventually going to be ran via php. I strongly recommend doing some research on prepared statements and refactoring your where methods. Currently if you were to use any sort of user generated values in your query for example, someone submitted their email on login and you were attempting to find their user with it, you will be open to attacks like SQL Injection. Refactoring this logic to make use of prepared statements and adding a bindings bucket to your class that stores the values that will eventually be used when executing the query should tighten up the security of your class. ex of what that might look like from a user's perspective:

$builder->table('users')->where('email', '=', '[email protected]'); 

$stmt = $pdo->prepare($builder->toSql()); 

$stmt->execute($builder->getBindings()); 

I suggest reading into PSR-12 standards for code style. Most IDE’s should have some configuration to enforce and even auto format to follow this.

The last thing I would recommend is if you are enjoying working on this, try to take it even further. You could try nested statements using recursion ex. “ … item = true or (otherItem = ‘this’ and anotherItem = 5) … ”. You could try integrating a database driver & connection into your class. The worst thing that will happen is you will learn something even if it doesn’t quite work out.

I have been programming for a handful of years now and in that time, I had never built myself a query builder. A couple weeks ago I decided to do this though. Not to say what I made is the correct way of doing it and I'm sure it's full of its share of issues, but I wanted to link it here for you to reference if you find that useful. I also recommend finding other query builder packages out there and just seeing how they have solved the same problem and compare it with your solutions. My attempt can be found here. If you REALLY wanted to dive in you could always check out the Laravel query builder package here but it might be a bit overwhelming.

The biggest thing I would focus on out of everything above is ensuring the interaction with the database is secure so if you take anything from this, please let that be the prepared statements.

Also it sounds like you are kicking ass for only being 16. Landing a job at that age already and putting yourself out there like this to improve yourself tells me you will do just fine.

2

u/Whole_Patience9125 Feb 12 '21

Thanks so much for all of this information :) I'll definitely take it all on board and re-post after I've fixed a lot of its issues and finished features like order of operation for where clauses :)

Your example also really helps, thanks again :)

3

u/Tazzl Feb 12 '21

Declare strict types and use types for method paramet.

3

u/bigByt3 Feb 12 '21

If you landed your first php developer position then your good enough. I'm literally twice your age and just started my first position as a developer last month. I came in with the same fears, and probably knowing less than what you do now about PHP. All of my schooling had been with software not web based programming.

My employer hired me because of my determination, not because of my knowledge. In this last month I've made leaps and bounds just through learning on the job. If its the right employer for you, they'll understand you've got a learning curve and give you the time to get up to speed, especially if you have a large code base to get up to speed with.

Onto the code itself, its clean, your naming conventions are self explanatory making your code self documenting without comments. I was able to follow along without any difficulty just by reading your code. Someone else already pointed out your use of join in a couple of the names so I wont get into that, just look into reserved words.

Also, as u/Bigdrums pointed out, PDO is your friend. SQL injection is a real thing, and there are plenty of ready built functions to help.

I have no idea what your job is, whats going to be required of you, or what the review process is with your code. You're already killing it though. Just take notes, pay attention to anything your co-workers tell you or any advice they give you and keep pushing to learn more.

2

u/Meek_braggart Feb 12 '21

At minimum you will have to deal with joins and aliases somehow.

1

u/shadywf Feb 12 '21

structure looks good. Although its harder to screw up classes. Great naming as well.

1

u/cichy86 Feb 12 '21

What with special columns name? I mean what if I want to add column with name order? I think that You should use names of columns in format: ‘column_name‘

1

u/chevereto Feb 12 '21

Try reducing the variability of your methods, to chop parameters. For example your where methods needs to pass the column, operator and value.

In your case, I would use "whereEquals", "whereIn", "whereNotIn", etc and internally handling the OR clause. Doing that will require more time, but you will notice eventually that it pays off.

Awesome that you picked a query builder for learning, is not that trivial and there are plenty query builders already out there which you can use for learning... Just keep in mind that stars/downloads won't always account for good code neither great choices, don't drive your learning experience giving authority to these metrics, look and judge on your own so the more you see, the more you will know.

1

u/chocolombia Feb 13 '21

Hi there, pretty neat code, beyond the pdo and types suggestions, I'll also check your identation, remember that tabs can interpreted differently, personally I use double spaced on my identation, you can set that up on your IDE