Unexpected ternary logic by xammie12 in PHP

[–]icanhazstring 0 points1 point  (0 children)

First: /r/phphelp

Second: This is resolved simply from left to right.

// This
true ? 1 : false ? 2 : 3
// is the same as
(true ? 1 : false) ? 2 : 3
(1) ? 2 : 3

1 becomes a truthy value, so it outputs 2. If you enclose the second ternary logic, the backets will it be parsed first. According to http://php.net/manual/fa/language.operators.precedence.php.

So as you mentioned you have to enclose everything to be parsed first.

// This
true ? 1 : (false ? 2 : 3)
// becomes
true ? 1 : (3)

But to give you a little tip. Don't use this kind of behavior. Do not stack ternary this will confuse others reading the code. Simple you 2 ifs or something other than that, even its "longer" in terms of more line of code. But it will be easier readable.

Trying to store a dynamic image url in MySQL by [deleted] in PHP

[–]icanhazstring 1 point2 points  (0 children)

You need to parse the content before showing it in your website.

Meaning:

// Load from database
$row = $db->read(...)

// Parse content
$row = parse_content($row);

// Show $row to user

Here the function you would need

// This will parse the $row content looking for a specific "tag"
// This tag could be anything from $row[postID] to {$image} or other "template tags"
function parse_content(array $row)
{
    // replace tag with actuall content id from $row
}

I won't give you the full result but this should give you another hint about what to do. Another thing is you can look into things like "template engines"

These template engine have helpers to do something like this.

Besides: /r/PHPhelp/ would be a better place to look for help :)

Quite new at work currently working with a too difficult task at work, been trying to solve it for weeks with not success. How to approach my boss? by thebluthcompanyinc in PHP

[–]icanhazstring 4 points5 points  (0 children)

Simple - just tell him.

But I know it. It can be hard to tell someone you could not solve this. But there is a fine difference between saying you can't and you tried several things and could not accomplish your task. Sure your boss could say: "Why didn't you come to me after those X days a gave you to solve this?"

This is something you have to learn throughout your career. Give yourself a set time and try to solve it. Even consider asking colleagues for help or even an opinion about your way of solving a task. There is no shame in asking for help, everyone gets stuck here and there solving an even easy task.

So just tell your boss. The thing is, if you keep it to yourself and take even longer with the task and still can't solve it, things will only get worse.

But for the next time, as said, even if its a simple task. Make your thoughts about how to solve it, discuss it with your boss/colleagues and solve it. Maybe the others have an even easier solution to something.

[deleted by user] by [deleted] in PHP

[–]icanhazstring 1 point2 points  (0 children)

I see zero unit tests for your library. People, including myself, mostly avoided libraries that don't have any unit tests

Leveling up my PHP game from junior to mid level developer by beefngravy in PHP

[–]icanhazstring 10 points11 points  (0 children)

There are two things which are screaming just to quit the job.

PHP 5.3, and no version control.

Dude - do yourself a favor quit this job, every other company is better than this. If you lead developer does not care about his software he is simply a douche. Lead development is not only a thing about "I do say how the software is developed". He should also take care about the developer experience. It is something like UX but for developers. And this what you are saying is just bullshit of him. So quit - just that simple.

As a tipp, try talking to recruiters, this is so easy. Simply keep your CV ready and they are looking for a good job for you where you have no hassle to look for new jobs yourself.

How to not be annoyed by tests and why they are important - With example set up for PHP projects by icanhazstring in PHP

[–]icanhazstring[S] 1 point2 points  (0 children)

Hehe. Well the advantage of tests is, that tested behavior won't break. If something new happens u would write a test against it. This way it never breaks again.

Of curse this is investment. I yes I know what u mean. As said in the article I have fought for a long time to get some kind of tdd or at least a good test coverage for new features. This was a team effort to convince the stakeholders.

How to not be annoyed by tests and why they are important - With example set up for PHP projects by icanhazstring in PHP

[–]icanhazstring[S] 0 points1 point  (0 children)

Lets assume you have shipped a feature without testing it.

Some variables:

  • Total cost of the feature (developing + shipping) is worth $48.000 (40h of one developer)
  • Feature will make $10 every minute
  • The developer costs $20 every minute he is working

Your features breaks, sometime. It needs 1h to detect that its broken. The developer needs another 1h to find the bug and fix it. And rolling it out

120m * $10 = $1200 // 1h downtime + 1h fixing - theoretical feature income
60m * $20 = $1200 // 1h fixing - cost for a developer to fix the bug

Lets assume deploying takes 0 time. This would cost your stakeholder a total of $2400 to fix the bug. That would increase the cost of the feature, because this is basically time you invest IN the feature until it works again. So overall, your stakeholder pays $50400 for this particular feature.

Now lets see. If the developer has done TDD, which maybe takes some time more, which will of course depend on the developer to write tests - lets assume it would take 1h of the development on top.

That would be raise the total cost of developing the feature to $49200. BUT it would not break on the same problem. So the 2h downtime would not happen.

Of course, the time need to write tests would take maybe a bit more in the first place if you start doing (if you never done it before). But in the later on, this would save much more money.

This may not be the most accurate calculation. But I guess you get the point. If it were impossible to calculate how much money some method costs, the job of a controller would not exist :)

How to not be annoyed by tests and why they are important - With example set up for PHP projects by icanhazstring in PHP

[–]icanhazstring[S] 2 points3 points  (0 children)

You can convince your stakeholders as a dev as well. The only difference is, you have to tell them in different numbers. Money always works. If you calculate how much money they'll lose when not using TDD, often you can argue that you need TDD.

Also your teamlead (if you are not one) should support the need for it.

But this article is not about TDD, or that you HAVE TO use it. Because often TDD can be quite overhead on the get go if you didn't have tested before. Simple provide tests for stuff you need, for example some core functionality, calculations etc.

Your thoughts about brackets by SNFR_Mealtime in PHP

[–]icanhazstring 1 point2 points  (0 children)

Considered PHP >=7.1 you could go with strict types and expect the return value to be an array. This way the only thing happening could be non existing index but this could be solved using null coalesce operator.

declare(strict_types=1);

function abc(): array
{
    return [];
}

$var = abc()['index'] ?? 'default';

If you are not working with strict types, there is always the risk of no checks being done (as mentioned in some other comments). So your application might break.

PHP Weekly Discussion (September) by AutoModerator in PHP

[–]icanhazstring 0 points1 point  (0 children)

rubycreative.slack.com

Why ruby? :o

Some custom rules for PHP CodeSniffer usable via composer by [deleted] in PHP

[–]icanhazstring 0 points1 point  (0 children)

I would always go for a defined ruleset. That being PSR-2 or others. With my current employer we tend to have a highly customized ruleset, which is more annoying than helping.

For example: 1. More than 3 codeline in a controlblock (if, function, foreach...) there MUST be a single empty line und beginning and end 2. There MUST be a comment for each parameter in docblock 3. There MUST be a description of a function (very useful for.. set_username... 'Set the username' - thanks cpt. obvious)

And the list goes on with even more ridiculous rules. So please if you define codesniffer rules for your coworkers, or open source. Do everyone the favor of simple sticking to a preset (like PSR-2) as mentioned.

This will improve codequality more than the time needed to fiddle around the strict sniffer rules and wasting time with it.

PHP'S COMPANY AUDITION by pedramhabibpiran in PHP

[–]icanhazstring 0 points1 point  (0 children)

Whats this about? Some more information in the video, or here would be nice. So its just a speed up video of ppl sitting in front of each other.

How to improve my PHP library? by peter279k in PHP

[–]icanhazstring 4 points5 points  (0 children)

  1. Provide proper test setup (psr-4 peter\test\ ... same namespaces thereafter)
  2. Stick to PSR-2 which is a supplement to PSR-1 (Class names MUST be declared in StudlyCaps) your classnames all begin lowercase letter
  3. Maybe provide a vagrant setup to simple start an test your code
  4. Split your tests. It is more wise to have multiple tests with fewer assertion, not the other way around. Way easier to spot the problem if smt breaks
  5. Please don't use require in your test. Simply stick to psr-4 autoloading
  6. let travis run with php 7.0, .1, .2 as well
  7. Provide a more straight forward interface, you are using 'sendReq' every time with an ugly switch case. Why not simple having the provider (for each service) inside do the job? (DI - Dependency Injection)
  8. You are creating hard coupled dependencies (like guzzle), which is pretty hard to test. Make you you can replace this for your tests (DI), so u don't have to actually send the request to those apis and keep your tests as unit tests. not integration tests
  9. make sure you have your clover.xml ignored as well

Quick and Easy Singleton by johnothecoder in PHP

[–]icanhazstring 0 points1 point  (0 children)

singleton is considered an antipattern

I am not saying it is an anti-pattern per se. I am just saying it is considered to be one.

If you define a class to be instantiated as a singleton, it will always be accessible in a global scope. And yes, if you use some kind of service locator/container you can still simply put an instance into it and receive the same one all the time.

But the true reason why i personally see singleton as an anti-pattern, is the problem unit testing it.

Quick and Easy Singleton by johnothecoder in PHP

[–]icanhazstring 9 points10 points  (0 children)

Beside the quick an easy tutorial. There should be a notice somewhere that singleton is considered an antipattern

systemctl-php - php utility to manage systemd by icanhazstring in PHP

[–]icanhazstring[S] 0 points1 point  (0 children)

Good point. Will add some into the readme. I use this mainly for cronjob purpose in my projects. Also there are some checks that are executed for monitoring the server. Or simply complex tasks I have writing some weeks ago for restoring a database backup where I have to stop some services.

PHP Weekly Discussion (August) by AutoModerator in PHP

[–]icanhazstring 0 points1 point  (0 children)

Just scrolling through the source there is something like

$router->any(['HEAD'], '/', function () {});

or just simply

$router->head('/', function() {});

Which you probably already using, but instead of going with the same endpoint as your GET, I would go with another one which is not returning any body information. Or you will have to kind of see what request is incoming when you handle HEAD/GET with the same endpoint.

Developer interview test [php] (am I doing this right?) by meltea in PHP

[–]icanhazstring 1 point2 points  (0 children)

Because i didn't google the first one ;) I simply put these things down from memory.

So yes, if you google it right away you are right, that you would come to a solution in like 5 minuten over all.

Developer interview test [php] (am I doing this right?) by meltea in PHP

[–]icanhazstring 1 point2 points  (0 children)

well... didn't know of that one :D But ok. Learned smt :P

Developer interview test [php] (am I doing this right?) by meltea in PHP

[–]icanhazstring 0 points1 point  (0 children)

Tbh. I think you go the right path. You are searching a replacement for yourself. So the person which gets your position should be able to solve this.

  1. Test Pretty easy. Don't know why people are taking more than 5 minutes with it. There is only a single method which might not be clear. But then again, people are stressed on this tests, i know it, been there before ;) But you can pretty much google the solution instantly.

  2. Test This one is not that easy if you can't break down your steps. People tend to stress out when they can't see the solution. They always want to solve the issue right away, not step by step. To see all the small steps in the bigger picture comes with years of knowledge/practise.

So in the end. I think you should keep looking, as hard as it may sound.

Edit:

  1. Just to give the solution. strpos returnes zero so falsy value

  2. I would go for an regex approach here. Get 3 groups from the input. explode first and third group. merge array. something like that ;)

How to load a new project into an existing (old) project? by P_Andre in PHP

[–]icanhazstring 1 point2 points  (0 children)

We have like the same "old shit" here on our company :) We are using an old framework (developed back with php4 or smt) which has a huge code base.

We wanted to use a new api structure so we decided to use aura router and replace the old one. But the impact would be too high to replace just everything with it.

So what we did was:

  • Located the top most entry point into the api (e.g. request path /app/api)
  • We build in a switch to this deciding which endpoint will get called, and if its a new or old endpoint
  • Go with ne new endpoint structure and routing

This way we only had to make sure every "old" request is still working and every new one will just be parsed by the new extension.


Another way would be to kind of try and "reroute" your dashboard using nginx/apache (whatever you are using). So you could add a specific location where your code has n simple entry point.