WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:00.286 --> 00:00:09.916 align:middle
Yeah, thanks for joining my
session, or my talk today.

00:00:10.736 --> 00:00:17.506 align:middle
As you can see, it's about static php code
analyzers and what I learned by using them.

00:00:18.376 --> 00:00:20.316 align:middle
My name is Nicole Cordes.

00:00:20.886 --> 00:00:23.076 align:middle
I'm a freelancer right now.

00:00:23.076 --> 00:00:28.396 align:middle
I started freelancing for PHP
applications and backend stuff this year.

00:00:28.956 --> 00:00:35.336 align:middle
I used to work in a Berlin agency and
next year I will worked for Tideways.

00:00:35.776 --> 00:00:41.716 align:middle
Maybe some of you know, it's like profiling php.

00:00:42.396 --> 00:00:51.306 align:middle
Well when I started to use static php code
analyzers, I noticed finally after a few weeks,

00:00:51.376 --> 00:00:55.146 align:middle
few months I changed my mind on how I code.

00:00:55.146 --> 00:00:59.056 align:middle
And I just want to talk about that and to maybe

00:00:59.116 --> 00:01:04.826 align:middle
to give you some reasons why you,
to start using code analyzers.

00:01:05.726 --> 00:01:14.196 align:middle
And before I want to talk about how I changed
my, uh, my, my, um, my expectations of my code,

00:01:14.196 --> 00:01:17.516 align:middle
I want to introduce you to
php static code analyzers

00:01:17.516 --> 00:01:23.006 align:middle
and the tools I'm using to analyze my php code.

00:01:24.216 --> 00:01:31.056 align:middle
And I chose four of the available tools maybe
you know, some of them, maybe you didn't hear

00:01:31.056 --> 00:01:37.716 align:middle
about it and maybe you even feel familiar
with one of those tools on your own.

00:01:38.496 --> 00:01:39.696 align:middle
So let's start.

00:01:39.906 --> 00:01:49.556 align:middle
When I, the first time I heard about static
php code analyzer was when PHPStan was released

00:01:49.556 --> 00:01:58.156 align:middle
on its GitHub page and this was my personal
getting in touch with PHP code analyzers.

00:01:58.946 --> 00:02:05.706 align:middle
As you can see PHPStan is available on GitHub.

00:02:05.826 --> 00:02:12.876 align:middle
You can just install it as a global
composer package and finally run it locally

00:02:12.876 --> 00:02:15.956 align:middle
on your machine and start analyzing projects.

00:02:16.886 --> 00:02:23.696 align:middle
I prepared some screenshots to
show you how it will look like.

00:02:24.406 --> 00:02:30.146 align:middle
In my point of view, it gives you a lot of
information when you run it, but it's strange.

00:02:30.146 --> 00:02:37.656 align:middle
The type-safety checks, like does
one function return the same type,

00:02:37.786 --> 00:02:40.146 align:middle
the other function expect and can deal with.

00:02:41.016 --> 00:02:50.056 align:middle
So as I told you, I have prepared multiple
php code analyzers, and each one in my point

00:02:50.056 --> 00:02:55.726 align:middle
of view has its own strength and it's own
way of showing and dealing with the php code.

00:02:56.816 --> 00:03:04.456 align:middle
So when you install it, PHPStan, and run
it locally on your project you just run it

00:03:04.456 --> 00:03:09.646 align:middle
on the cli and get some output grouped by class.

00:03:10.256 --> 00:03:14.096 align:middle
And as you can see, you get
some information about ???

00:03:14.266 --> 00:03:20.066 align:middle
names, you get some information
about the return type statements.

00:03:20.676 --> 00:03:24.686 align:middle
You will get a lot of information,
I'm sure in your project.

00:03:25.376 --> 00:03:35.836 align:middle
And um, I chose some of them in my later slides
and will not stick too much to the output,

00:03:35.936 --> 00:03:39.916 align:middle
but I just want to show you
how the tools will look

00:03:39.916 --> 00:03:45.056 align:middle
like when you're using them in your own project.

00:03:46.296 --> 00:03:52.686 align:middle
The second one after dealing with PHPStan
I found there are different projects

00:03:52.686 --> 00:03:56.196 align:middle
and services dealing with PHP code analyzers.

00:03:56.826 --> 00:04:01.736 align:middle
The second one I want to introduce
is the PHPMD the PHP Mess Detector.

00:04:02.426 --> 00:04:05.646 align:middle
Maybe some of you already
heard that because this is,

00:04:05.746 --> 00:04:09.086 align:middle
I think very long-living project already.

00:04:09.786 --> 00:04:15.306 align:middle
Maybe even so long-living that it's
already was forgotten that it's available.

00:04:15.306 --> 00:04:19.236 align:middle
But it still exists and still maintained.

00:04:19.236 --> 00:04:28.966 align:middle
And what I like most on the PHP Mess
Detector, it can handle your code structure

00:04:28.966 --> 00:04:35.126 align:middle
and measurements, I think in a more
better way than PHPStan can do it,

00:04:35.126 --> 00:04:40.316 align:middle
or at least output the information about
those measurements and code analyzations.

00:04:41.306 --> 00:04:46.576 align:middle
It's available as composer
package and PHAR file as well.

00:04:47.126 --> 00:04:54.986 align:middle
And if you execute it you get some more or
less written text, as well grouped by file.

00:04:54.986 --> 00:05:01.406 align:middle
It's not that nice way like PHPStan
outputs, but you can find all necessary

00:05:01.406 --> 00:05:07.036 align:middle
and available information in this text and
you can even change the kind of output.

00:05:07.036 --> 00:05:13.186 align:middle
PHP Mess Detector can output an
HTML page where you can navigate

00:05:13.446 --> 00:05:17.286 align:middle
through open issues and files as well.

00:05:17.286 --> 00:05:21.596 align:middle
So depending on what you need,
you can switch the output.

00:05:22.196 --> 00:05:29.246 align:middle
The third one I want to introduce
is some kind of service.

00:05:29.306 --> 00:05:37.636 align:middle
It's called Code Climate and
it's subtitle it has itself is:

00:05:37.636 --> 00:05:40.596 align:middle
"Automated code review and quality analytics".

00:05:41.296 --> 00:05:45.866 align:middle
As you can see, this is published
as a code analyzation

00:05:45.866 --> 00:05:49.526 align:middle
as a service, and it has a nice website.

00:05:49.526 --> 00:05:54.996 align:middle
You can connect with your GitHub account and
you can just use all your open source projects

00:05:54.996 --> 00:06:00.166 align:middle
for free and couple it to your
GitHub account and GitHub projects.

00:06:00.196 --> 00:06:08.446 align:middle
So this is quite nice to even use it in
the existing projects because you just hook

00:06:08.446 --> 00:06:13.676 align:middle
in with your GitHub, give some credentials
to the Code Climate application,

00:06:14.146 --> 00:06:21.536 align:middle
and you can start right away analyzing
available projects with just one or two clicks.

00:06:21.536 --> 00:06:29.246 align:middle
After you add your existing project, you
will get some really nice information.

00:06:29.246 --> 00:06:31.996 align:middle
You can see some nice summary.

00:06:32.516 --> 00:06:41.706 align:middle
They use the A to F level to just give you an
overview how maintainable your code currently

00:06:41.706 --> 00:06:43.746 align:middle
looks like and what they found out.

00:06:44.286 --> 00:06:48.076 align:middle
They even can analyze the duplicate code.

00:06:48.146 --> 00:06:55.256 align:middle
So you will get some notification if you
have multiple files using the same code

00:06:55.256 --> 00:07:03.646 align:middle
and you can just refactor your code and exclude
duplicate code into own classes and object.

00:07:04.506 --> 00:07:13.176 align:middle
So this is, I think this is a more nicely
way to get those information then on the cli.

00:07:13.176 --> 00:07:20.096 align:middle
So I'm more or less using the services
that I'm introducing to you right now.

00:07:20.946 --> 00:07:29.816 align:middle
And just to give you an overview, how you get,
how the results are more structured, presented.

00:07:29.916 --> 00:07:37.336 align:middle
You can even see the codebase
where the information is available.

00:07:37.746 --> 00:07:46.536 align:middle
So you can not just see some lines of php files
and some information about your code structure,

00:07:46.986 --> 00:07:52.456 align:middle
but you can even see the code
and maybe compare the information

00:07:52.456 --> 00:07:54.816 align:middle
or the remark and the code itself.

00:07:54.996 --> 00:08:03.116 align:middle
So it get, you get a better overview what
you have to do to resolve the remarks.

00:08:03.116 --> 00:08:11.116 align:middle
And you are able to even group depending
on some criteria so you can just say, okay,

00:08:11.116 --> 00:08:19.106 align:middle
I want to care about naming things and want
to ignore all the complexity stuff like that.

00:08:19.106 --> 00:08:23.976 align:middle
So I think it's worth to have a look at this.

00:08:24.096 --> 00:08:28.866 align:middle
And to be honest, my preferred
one is the SonarCloud.

00:08:29.576 --> 00:08:32.736 align:middle
Maybe you even heard of SonarQube before.

00:08:33.216 --> 00:08:42.566 align:middle
SonarCloud is the SonarQube as a service, it's
free for open source as well and it's even,

00:08:43.026 --> 00:08:48.876 align:middle
now it's not that easy to run it because you
need some preparation in your repository,

00:08:48.876 --> 00:08:51.286 align:middle
but you connect with your
GitHub account as well.

00:08:51.836 --> 00:08:54.526 align:middle
And finally you're ready to start.

00:08:54.526 --> 00:08:59.766 align:middle
You have to add some configuration file
and some execution in your project.

00:08:59.936 --> 00:09:08.396 align:middle
Currently on my project I run some building
process or testing process on TravisCI.

00:09:08.396 --> 00:09:14.716 align:middle
I think TravisCI or GitlabCI is
known by everybody in this room.

00:09:15.236 --> 00:09:20.216 align:middle
And one step in the CI configuration
is to send...

00:09:20.376 --> 00:09:24.576 align:middle
to generate the information
and to send it to SonarCloud.

00:09:25.356 --> 00:09:36.806 align:middle
And SonarCloud itself offers like Code
Climate, a nice overview in some website style

00:09:36.906 --> 00:09:39.986 align:middle
of course, it's a web application.

00:09:40.766 --> 00:09:55.426 align:middle
And you have the information about bugs so that
trying really to analyze php on multiple levels

00:09:55.426 --> 00:10:00.826 align:middle
of php code you have bugs,
you have even vulnerabilities.

00:10:00.826 --> 00:10:03.996 align:middle
So they have kind of security checks as well.

00:10:04.806 --> 00:10:06.386 align:middle
You have those code smells.

00:10:06.756 --> 00:10:09.216 align:middle
It's the things I want to talk about today.

00:10:09.646 --> 00:10:20.086 align:middle
And um, you have some section
even for PHPUnit code coverage.

00:10:20.526 --> 00:10:26.256 align:middle
So you can even include your php code
coverage reports and the SonarQube

00:10:26.256 --> 00:10:33.176 align:middle
and SonarCloud information and see
everything for one project on just one page.

00:10:33.676 --> 00:10:42.586 align:middle
Just to give you some other expressions,
we can then open the issues so you get more

00:10:42.586 --> 00:10:45.666 align:middle
or less a list of issues found for the project.

00:10:46.256 --> 00:10:53.826 align:middle
And um, well you have those, you have
those grouping and check marking options

00:10:53.826 --> 00:10:57.546 align:middle
and sorting filters as well
as on the other page.

00:10:58.356 --> 00:11:06.026 align:middle
And when you get one step deeper, uh,
you even see more, more lines of code

00:11:06.026 --> 00:11:08.266 align:middle
of your project for each remarks.

00:11:08.266 --> 00:11:16.606 align:middle
So it's, really underlying the problems and you
can get some more information about the remarks.

00:11:16.606 --> 00:11:23.236 align:middle
They have a huge database where they all
describe what, what remarks they are offering

00:11:23.706 --> 00:11:31.566 align:middle
to your code and this is really a nice way
to be informed and, uh, get some information

00:11:31.566 --> 00:11:34.776 align:middle
and impressions how static code is analyzed.

00:11:36.336 --> 00:11:45.166 align:middle
And because I just used some, some newly
introductory words, I want to introduce you

00:11:45.166 --> 00:11:47.476 align:middle
to the world of static code analyzation.

00:11:48.526 --> 00:11:52.976 align:middle
First of all, we have the topic of measurements.

00:11:53.176 --> 00:11:57.536 align:middle
Measurements means it's about
numbers more or less.

00:11:58.266 --> 00:12:03.426 align:middle
So we have a remark, so information
about our variable names.

00:12:04.126 --> 00:12:08.266 align:middle
We can, we can execute checks:
are the names too short?

00:12:08.266 --> 00:12:09.806 align:middle
Are the names too long?

00:12:10.286 --> 00:12:15.876 align:middle
Do they stick to lower camel-case names?

00:12:16.716 --> 00:12:21.656 align:middle
Other services analyze method names.

00:12:21.756 --> 00:12:27.486 align:middle
So they do they have a proper length?

00:12:27.996 --> 00:12:36.646 align:middle
To methods names which are too short, should
maybe be extended to be more readable.

00:12:38.216 --> 00:12:46.186 align:middle
There are some tools even can check the uses of
the constructor method instead of the class name

00:12:46.186 --> 00:12:54.106 align:middle
and they can even go a step further and
check the return type of your method

00:12:54.366 --> 00:13:03.916 align:middle
and offer some suggestion to rename the
method from getFoo() to isFoo() or hasBar()

00:13:04.226 --> 00:13:07.426 align:middle
if your method called getFoo()
returns a boolean.

00:13:07.906 --> 00:13:14.866 align:middle
So there are a couple of information
you get about just naming things.

00:13:15.696 --> 00:13:18.026 align:middle
And as I said, it's all about numbers.

00:13:18.956 --> 00:13:25.226 align:middle
Those tools are, also measure
your method length.

00:13:25.286 --> 00:13:28.146 align:middle
So how many lines of code does your method have?

00:13:28.226 --> 00:13:31.226 align:middle
How many lines of code does your class have?

00:13:31.946 --> 00:13:34.646 align:middle
How many parameters does your method have?

00:13:34.776 --> 00:13:40.746 align:middle
And you can, you can configure
your personal values

00:13:40.746 --> 00:13:46.986 align:middle
or you can configure your personal likings and
say, okay, I want to stick to parameters list

00:13:46.986 --> 00:13:51.936 align:middle
of three parameters because if I need
more parameters, maybe the method that's,

00:13:52.116 --> 00:13:58.236 align:middle
that's too much and I need to refactor
my code and group maybe parameters

00:13:58.236 --> 00:14:01.186 align:middle
into other objects and stuff like that.

00:14:01.946 --> 00:14:08.016 align:middle
It even has some measurements about field
count and public field count of public methods.

00:14:08.016 --> 00:14:13.536 align:middle
So like saying your class has a lot of
public method, you can just configure,

00:14:13.586 --> 00:14:22.956 align:middle
okay on my classes should have five public
methods, otherwise they doing stuff too complex

00:14:22.956 --> 00:14:28.596 align:middle
and I need to rethink or restructure
my objects and class information.

00:14:29.126 --> 00:14:39.336 align:middle
Then they are analyzers concerning
your code, your php code.

00:14:40.706 --> 00:14:45.906 align:middle
So one of the analyzation is: do
you have a lot of commented code?

00:14:46.406 --> 00:14:50.976 align:middle
Because I think all of us use
some kind of versioning system.

00:14:50.976 --> 00:14:55.586 align:middle
We can just simply remove code,
there's no need to comment code out.

00:14:56.066 --> 00:15:01.496 align:middle
And, personally I would say
there's no need to have commented

00:15:01.496 --> 00:15:04.486 align:middle
out code for, maybe "I need it later".

00:15:04.486 --> 00:15:05.696 align:middle
So just remove that.

00:15:05.696 --> 00:15:12.446 align:middle
You can restore code according to your
local history in your versioning system.

00:15:13.566 --> 00:15:18.276 align:middle
They analyze how many return
statements does a method have?

00:15:18.466 --> 00:15:25.756 align:middle
Because when you have a lot of return statements
throughout your, um, throughout your method,

00:15:25.756 --> 00:15:33.976 align:middle
maybe it's not that practical and it
gets too complicated to read your method

00:15:33.976 --> 00:15:37.766 align:middle
and to understand where does
which return come from.

00:15:38.386 --> 00:15:43.036 align:middle
And so my recommendation would
be return early but only once.

00:15:44.036 --> 00:15:48.406 align:middle
So more or less all my methods
have an early return.

00:15:48.976 --> 00:15:56.226 align:middle
But afterwards I just have one final return
and storing the value in, some variables.

00:15:56.936 --> 00:16:04.096 align:middle
Or even I don't need some variables if
the return can be passed from conditions.

00:16:04.496 --> 00:16:10.856 align:middle
So I can just, I don't need to use extra
variables to store the return value

00:16:11.186 --> 00:16:14.826 align:middle
and then have the last statement return value.

00:16:15.256 --> 00:16:24.176 align:middle
But I can just say: okay, return if
empty or if this and that is ????.

00:16:24.516 --> 00:16:35.216 align:middle
And if you return early, I consider or I would
recommend to prevent superfluous else parts.

00:16:35.216 --> 00:16:43.376 align:middle
I think we all have written code where we have,
if some condition is fulfilled, return true

00:16:43.946 --> 00:16:50.606 align:middle
and then I'm, I used to, I used
to stick to else, return false.

00:16:51.156 --> 00:16:55.986 align:middle
But if you have an early return, you don't
need the else part, you can just say, okay,

00:16:55.986 --> 00:17:01.376 align:middle
if I'm not inside this condition that
returns true, I just want to return false.

00:17:01.536 --> 00:17:06.076 align:middle
There's no need for the else part.

00:17:06.476 --> 00:17:14.746 align:middle
And um, when you're dealing with return
type statements, and this is, maybe you,

00:17:14.746 --> 00:17:22.086 align:middle
you remember I said the PHPStan, is a really
good tool to deal with return type statement

00:17:22.086 --> 00:17:27.276 align:middle
and dealing with: does the return
type match the expectation.

00:17:27.896 --> 00:17:33.046 align:middle
When you have too many return type
possibilities, those tools cannot deal

00:17:33.046 --> 00:17:37.366 align:middle
with the proper, with the proper
type-hinting and say, okay,

00:17:37.576 --> 00:17:42.206 align:middle
I'm not sure what this value
here is and maybe it's not valid

00:17:42.206 --> 00:17:45.506 align:middle
and they give you a wrong, false positives.

00:17:46.086 --> 00:17:51.106 align:middle
So stick, or try at least
stick to one return type.

00:17:51.166 --> 00:17:57.596 align:middle
And my personal recommendation would be
try even to prevent nullable return types.

00:17:57.786 --> 00:18:04.996 align:middle
So really use empty arrays use
empty objects, stuff like that just

00:18:04.996 --> 00:18:11.506 align:middle
to have one return type in one method.

00:18:12.366 --> 00:18:19.176 align:middle
As I said already, they, the tools
are useful to give you numbers

00:18:19.176 --> 00:18:22.306 align:middle
of how many methods you are working.

00:18:22.716 --> 00:18:28.806 align:middle
If you have the recommendation or if
you have the information that's that one

00:18:28.806 --> 00:18:34.526 align:middle
of your class uses too many methods, you
maybe should think about multiple objects

00:18:34.526 --> 00:18:40.386 align:middle
or you maybe should think about
refactor the object and split task

00:18:40.696 --> 00:18:47.906 align:middle
into two separate objects just to reduce
the numbers of methods in one class.

00:18:49.066 --> 00:18:56.296 align:middle
One other recommendation from the
analyzers is: throw dedicated exceptions

00:18:56.636 --> 00:18:59.286 align:middle
that can be caught outside the code itself.

00:18:59.406 --> 00:19:07.126 align:middle
Don't throw, don't throw RuntimeExceptions,
but throw ConfigurationIsNotSetException

00:19:07.126 --> 00:19:12.426 align:middle
or ValueIsNotExpectedException
that can be directly caught

00:19:12.666 --> 00:19:17.966 align:middle
by some other code around those methods.

00:19:17.966 --> 00:19:22.056 align:middle
And for multiple reasons the recommendation

00:19:22.116 --> 00:19:28.426 align:middle
of those static php code
analyzers is avoid static calls.

00:19:29.446 --> 00:19:35.826 align:middle
Just try to use dependency object
that have own method you can call

00:19:35.826 --> 00:19:40.426 align:middle
and own return statements
that you can deal with.

00:19:40.716 --> 00:19:48.426 align:middle
This makes working with your code a lot
easier when you have to deal with testing.

00:19:49.076 --> 00:19:58.336 align:middle
So static calls in the manner of you want to
test your code, you don't want to have that.

00:19:59.036 --> 00:20:02.146 align:middle
You don't want to have static
calls in your code.

00:20:03.626 --> 00:20:10.176 align:middle
And my favorite topics of all this
php code analyzers is a whole topic

00:20:10.176 --> 00:20:12.296 align:middle
about complexity of your code.

00:20:12.586 --> 00:20:20.606 align:middle
So complexity of your code, it's
about how hard is your code readable

00:20:20.846 --> 00:20:23.926 align:middle
or how hard is it to understand your code.

00:20:24.636 --> 00:20:32.106 align:middle
If you have a lot of structure controls
inside one method - so like if else, foreach,

00:20:32.716 --> 00:20:38.556 align:middle
switch and you have really huge
kind of levels inside a method -

00:20:38.626 --> 00:20:41.636 align:middle
your method is not understandable anymore

00:20:41.636 --> 00:20:45.136 align:middle
and you have too many branches
and too many conditions.

00:20:45.776 --> 00:20:52.246 align:middle
So this is really, really good
information from the static tool analyzers.

00:20:52.696 --> 00:20:57.386 align:middle
I got, in my first weeks using them,

00:20:57.996 --> 00:21:06.586 align:middle
to learn that they completely changed the way
I finally looked at code because, as I said,

00:21:06.636 --> 00:21:11.806 align:middle
each control is considered as one
logical point: they have a number

00:21:11.856 --> 00:21:16.206 align:middle
that increased per control structure.

00:21:16.666 --> 00:21:23.536 align:middle
And even if you have, for example, an if
statement and using some else or or inside one

00:21:23.536 --> 00:21:28.646 align:middle
if statement, even those logical
operators increase the count of complexity.

00:21:29.626 --> 00:21:35.926 align:middle
And even ternary operators and
null coalesce operators account

00:21:35.926 --> 00:21:41.566 align:middle
as own increasing point for
the, for the complexity.

00:21:42.436 --> 00:21:48.286 align:middle
So if you have a really long
method with huge conditions

00:21:48.286 --> 00:21:52.116 align:middle
and use logic controls, it's
hard to understand them.

00:21:52.116 --> 00:21:57.326 align:middle
And I'm sure nobody then nobody else and
you will understand the code properly.

00:21:58.626 --> 00:22:06.706 align:middle
And so the recommendation from my point
of view is merge nested if statements

00:22:06.946 --> 00:22:15.656 align:middle
like you don't need to open if condition one,
then if condition two then if condition three,

00:22:15.656 --> 00:22:19.116 align:middle
but you can just group them
into one if statement.

00:22:19.526 --> 00:22:21.426 align:middle
So you can merge the if conditions.

00:22:23.176 --> 00:22:29.916 align:middle
And use helper functions to resolve
those conditions in the first time.

00:22:29.916 --> 00:22:35.656 align:middle
So it's more readable to have three
proper name method for your conditions.

00:22:36.046 --> 00:22:43.736 align:middle
So if: is condition 1 met or is condition 2 met
or is condition 3 met, it's much more readable

00:22:43.736 --> 00:22:46.936 align:middle
than having those three conditions on one line

00:22:46.936 --> 00:22:51.256 align:middle
and not having a clue: okay,
what is this test about?

00:22:51.256 --> 00:22:52.726 align:middle
What is this condition about?

00:22:52.726 --> 00:22:57.586 align:middle
So we factor conditions in own helper
method and named them properly.

00:22:57.586 --> 00:23:06.706 align:middle
This really helps you a lot to understand
what's going on in your own code.

00:23:06.986 --> 00:23:18.536 align:middle
So after I introduced you on some magic php code
conclusions, I want to give you my conclusion,

00:23:18.536 --> 00:23:28.886 align:middle
how my code changed via using the PHP code
analyzation and what I try to do now for my code

00:23:28.886 --> 00:23:37.166 align:middle
and what I maybe want you to ask
or you offer to test on your own.

00:23:38.096 --> 00:23:43.656 align:middle
Like I already said, I pay more
attention in naming things.

00:23:43.656 --> 00:23:47.756 align:middle
I know the, one of the main issues
in development is naming things.

00:23:48.526 --> 00:23:55.386 align:middle
And when not common with naming things and we
have still used problems with naming things.

00:23:55.996 --> 00:24:03.356 align:middle
So it forced me to pay more attention
in how do I name my variables.

00:24:03.356 --> 00:24:11.666 align:middle
And even more attention in how do I
name my methods to make it more readable

00:24:11.666 --> 00:24:20.116 align:middle
and to make it more speakable so that even other
developers who use my code - or you have to work

00:24:20.206 --> 00:24:27.266 align:middle
with my code - understand what this method
is about, without even having to look

00:24:27.266 --> 00:24:29.916 align:middle
into the method, but just by reading the name.

00:24:30.476 --> 00:24:40.596 align:middle
As I said I already refactor conditions and own
methods just to proper combine them into one

00:24:40.596 --> 00:24:47.776 align:middle
if statement and have all the single
conditions and own readable methods

00:24:48.156 --> 00:24:55.486 align:middle
and even my methods just return
this simply line of code.

00:24:55.586 --> 00:25:01.746 align:middle
But because I'm able to read the method
name, I know what this check is about

00:25:01.746 --> 00:25:05.446 align:middle
and I know why I'm checking this
and this explicit condition.

00:25:05.446 --> 00:25:11.746 align:middle
So, there's this one thing I try
to do each time and as I said,

00:25:12.416 --> 00:25:22.426 align:middle
I really try to prevent multiple return
types because I think and, and those, those,

00:25:22.426 --> 00:25:33.816 align:middle
those two paths go hand-in-hand because
I can combine multiple conditions,

00:25:34.136 --> 00:25:40.816 align:middle
I can just have one early return
and then go on with my proper code

00:25:40.816 --> 00:25:48.696 align:middle
of application controller logics and have
just one, one line with some conditions

00:25:48.786 --> 00:25:51.906 align:middle
that can catch up the preconditions
of this method,

00:25:51.956 --> 00:25:55.016 align:middle
if the preconditions are
not met I can simply return.

00:25:55.436 --> 00:26:01.636 align:middle
And afterwards I can just go on and I'm,
I can be sure all my preconditions are met

00:26:01.746 --> 00:26:05.946 align:middle
because I named the preconditions
and already checked it.

00:26:06.656 --> 00:26:13.466 align:middle
And well, as I already told you, I
really try to use and force myself

00:26:13.466 --> 00:26:22.106 align:middle
to only use one return type per method and to
even to prevent return nullable return types

00:26:22.206 --> 00:26:29.336 align:middle
because this makes dealing with, with the return
values a lot of easier even in the, um, in,

00:26:29.336 --> 00:26:33.706 align:middle
in the parent code above, uh, outside my method.

00:26:34.186 --> 00:26:38.286 align:middle
So I, I don't have to deal
with multiple values anymore.

00:26:38.286 --> 00:26:45.386 align:middle
I can just say, okay, if this exists or if this
property has some kind of value, I can go on.

00:26:45.766 --> 00:26:49.886 align:middle
Otherwise I don't care, I
don't have to care about that.

00:26:50.676 --> 00:26:56.666 align:middle
So maybe what you think the
conclusion is: I really start thinking

00:26:56.666 --> 00:27:03.926 align:middle
about code before I writing it and I think this
is the most important point in my point of view.

00:27:04.416 --> 00:27:06.376 align:middle
I'm not just writing code.

00:27:06.376 --> 00:27:09.066 align:middle
I think about how to deal with the code.

00:27:09.066 --> 00:27:14.006 align:middle
I think about how, how I can
better maintain my code even

00:27:14.006 --> 00:27:17.496 align:middle
in the next few days or few weeks or few month.

00:27:17.776 --> 00:27:22.306 align:middle
And I think it's even easier for others
to deal with the code I'm writing.

00:27:23.126 --> 00:27:32.176 align:middle
And the benefits of thinking about how to
write code is I think my classes and concepts

00:27:32.236 --> 00:27:38.626 align:middle
for applications got a lot of better
because I just care about naming thing.

00:27:38.626 --> 00:27:44.966 align:middle
I just care about concepts because I
want to group them in the proper task

00:27:45.436 --> 00:27:48.976 align:middle
and proper the proper groups of methods.

00:27:49.436 --> 00:27:57.606 align:middle
And as I already told you, I think my code is
more readable because I have more speaking code.

00:27:57.606 --> 00:28:02.656 align:middle
I don't need to get into method just
to understand what they're doing.

00:28:03.296 --> 00:28:06.826 align:middle
And my code gets a lot of testable.

00:28:07.416 --> 00:28:13.566 align:middle
So fighting tests, unit tests,
integration tests gets a lot easier

00:28:13.656 --> 00:28:20.466 align:middle
because I have all those methods available
and can really test everything on its own

00:28:20.466 --> 00:28:27.456 align:middle
and I can decouple my objects from,
from the testing point of view

00:28:27.456 --> 00:28:34.436 align:middle
and can offer some mocking objects there because
I use dependency injection and stuff like that.

00:28:34.436 --> 00:28:38.846 align:middle
So even testing gets a lot
easier with the proper code.

00:28:40.186 --> 00:28:51.306 align:middle
After I give you so many information about
why you should change the way you were working

00:28:51.306 --> 00:28:55.816 align:middle
with code and why you should
start using php static analyzer,

00:28:56.836 --> 00:29:01.526 align:middle
I just want to give you some
words of - I call it warnings.

00:29:03.046 --> 00:29:07.416 align:middle
So when, when I started working
with a php code analyzers,

00:29:07.416 --> 00:29:11.836 align:middle
my initial goal was to resolve everything.

00:29:12.116 --> 00:29:18.106 align:middle
So every remark has some valid point
of view and I need to change my code

00:29:18.106 --> 00:29:21.956 align:middle
to just resolve every remarks
from each and every tool.

00:29:22.536 --> 00:29:31.046 align:middle
And, I, I really would ask you, or
what I noticed after I did months

00:29:31.376 --> 00:29:35.466 align:middle
with static code analyzers
is: stop over-engineering.

00:29:35.466 --> 00:29:41.456 align:middle
You don't need to resolve any problem
or any remark given by this tools.

00:29:42.296 --> 00:29:50.766 align:middle
I noticed not all remarks are really valid
because you may be depending on some kind

00:29:50.766 --> 00:29:56.776 align:middle
of frameworks and this is your code is
exactly that way the framework expected

00:29:56.826 --> 00:30:04.366 align:middle
to be then you don't have any chance to resolve
those problems coming from the code analyzers.

00:30:05.026 --> 00:30:11.566 align:middle
Or depending on your time and money in the
project, that's maybe not that possible to work

00:30:11.696 --> 00:30:20.106 align:middle
on the project anymore or to have the time to
make a clean up after you've written the code.

00:30:20.676 --> 00:30:27.956 align:middle
But I really would like to ask you
if you see, if you see the output

00:30:27.956 --> 00:30:34.546 align:middle
of the php static code analyzer and you don't
have the time or you don't have the money

00:30:34.846 --> 00:30:40.386 align:middle
to clean up your code, just use
those information for your new code.

00:30:40.646 --> 00:30:46.236 align:middle
So, I can totally understand
that there are projects and,

00:30:46.526 --> 00:30:52.786 align:middle
where you don't have maybe the effort, maybe
the time, maybe the money, I don't know.

00:30:52.786 --> 00:30:58.196 align:middle
There are multiple reasons why you
don't work on existing code anymore.

00:30:58.736 --> 00:31:02.206 align:middle
But knowing those issues
and not learning from them,

00:31:02.206 --> 00:31:07.926 align:middle
I think it's the most worst case you
can have So if you get the information

00:31:07.926 --> 00:31:16.866 align:middle
that some older code has problems or some older
code should be refactored, use those information

00:31:17.226 --> 00:31:27.906 align:middle
for your new code to prevent those, uh, those,
uh, uh, errors, those problems you had before.

00:31:29.696 --> 00:31:39.876 align:middle
And, well, as I said, even sometimes in my
code analyzations of my open source projects,

00:31:40.316 --> 00:31:45.206 align:middle
I noticed there are really wrong
false positive information.

00:31:45.206 --> 00:31:49.706 align:middle
So, the tool has given me
some kind of information,

00:31:49.996 --> 00:31:53.046 align:middle
it's about complexity or
it's about naming things.

00:31:53.046 --> 00:31:55.966 align:middle
And I think, okay, no this is not that valid.

00:31:56.176 --> 00:32:03.356 align:middle
For example, one of the remarks
from the SonarQube code analyzer is

00:32:03.836 --> 00:32:13.086 align:middle
to store every string value that you use
multiple times within one class in constants,

00:32:13.376 --> 00:32:18.266 align:middle
to refactor the code and use
constants instead of the string values.

00:32:18.786 --> 00:32:21.296 align:middle
And this is something I cannot agree with.

00:32:21.646 --> 00:32:26.396 align:middle
So this is not a valuable recommendation.

00:32:26.766 --> 00:32:33.256 align:middle
And, I, I'm more or less excluded
this checks from my project

00:32:33.416 --> 00:32:37.896 align:middle
because I think this is some
information I don't want to have each time

00:32:37.896 --> 00:32:40.566 align:middle
because this is something
I won't change in my code.

00:32:41.686 --> 00:32:49.816 align:middle
And as I said, all the knowledge you
gained from this PHP code analyzation,

00:32:50.166 --> 00:32:54.126 align:middle
even if you change existing
code or not, the knowledge,

00:32:54.126 --> 00:32:58.206 align:middle
use it for upcoming code
to write really clean code.

00:32:58.206 --> 00:32:59.396 align:middle
Think about it.

00:32:59.396 --> 00:33:05.956 align:middle
Think about how others may see this
code and how other would understand,

00:33:06.406 --> 00:33:09.086 align:middle
or if others would understand your code.

00:33:09.436 --> 00:33:18.606 align:middle
And when you're working on open source projects
or have public available projects on your own,

00:33:18.846 --> 00:33:23.426 align:middle
just think about contribution,
make it more easier to contribute,

00:33:23.426 --> 00:33:30.166 align:middle
make your code more understandable and
start really using php code analyzers.

00:33:33.236 --> 00:33:41.866 align:middle
So I hope you get some impression or you get
some useful information about code analyzing.

00:33:42.616 --> 00:33:44.106 align:middle
Thanks for joining.

00:33:44.106 --> 00:33:53.486 align:middle
And maybe one or two have some
questions or I even prepared some output.

00:33:53.486 --> 00:33:58.826 align:middle
So if you want to get into some
output information, we can have a look

00:33:58.826 --> 00:34:18.456 align:middle
in the real life example, if you're interested.

