PHP Reddit – Telegram
PHP Reddit
34 subscribers
289 photos
37 videos
24.8K links
Channel to sync with /r/PHP /r/Laravel /r/Symfony. Powered by awesome @r_channels and @reddit2telegram
Download Telegram
Code Quality

Hi guys, I hope you are all doing good.

How do you guys ensure code quality on your PHP application?
I am currently leading(a one man team🤣) the backend team for a small startup using PHP and Laravel on the backend.
Currently, we write integration test(with Pest), use PHPstan for static analysis(level 9), Laravel Pint for code style fixing.

I have recently been wondering how else to ensure code quality on the backend.
How else do you guys enforce / ensure code quality on your applications?
Are there specific configurations you use alongside these tools, or are there even some other tools you use that isn't here?
Thanks in advance, guys.

https://redd.it/1mi53j1
@r_php
Do you update flex recipes when upgrading Symfony version?

I just updated my app from Symfony 7.2 to 7.3. When using the `composer recipes:update` command, i get a big list of recipes to update. However, the process is annoying as i have to do it one by one with a commit after each. How important is this step and how do you do it?

https://redd.it/1mia8eh
@r_php
Excessive micro-optimization did you know?

Some built-in functions have compiler optimized versions that avoid a lot of the internal overhead of the PHP engine. By importing them in source files (e.g., use function array_map) or by prefixing them with the global namespace separator, for example \is_string($foo):

<?php

namespace SomeNamespace;

$now1 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$result1 = strrev("Hello, World!");
}
$elapsed1 = microtime(true) - $now1;
echo "Without import: " . round($elapsed1, 6) . " seconds\n";

$now2 = microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$result2 = \strrev("Hello, World!");
}
$elapsed2 = microtime(true) - $now2;
echo "With import: " . round($elapsed2, 6) . " seconds\n";

For this tight loop, it results in an 86.2% performance increase. This was tested with OPcache enabled. You will se smaller performance gains with it disabled.

Will this affect real world applications? Most likely not by much.

https://redd.it/1milupj
@r_php
AI & Programming

PHPStorm, my preferred IDE uses AI to predict what I’m writing. It works quite well but it does have me questioning the future of my job security and hobby.

While currently AI produces often buggy and difficult to maintain spaghetti, how much longer until this is no longer the reality?

Is there anything I should be doing to prepare for this?

https://redd.it/1mingtk
@r_php
assetMapper and fonts

Hi,

I'm trying to import a google font.

php bin/console importmap:require @fontsource/oswald

This works fine, but after, i need to replace this with the local version, in my css

import url('https://fonts.googleapis.com/css2?family=Oswald:wght@200..700&display=swap');

i've tried

@import url('@fontsource/oswald/index.min.css');

or

@import url('@fontsource/oswald');

But it does not work..

How can i do that ?

https://redd.it/1miy6l1
@r_php
Configuring Renovatebot for upgrading Symfony?

I'm setting up Renovatebot to update my packages in a Symfony app, but i'm a bit confused about the Symfony packages.

I want to make a group so it will update them all together when doing Symfony upgrade. But how to make it change this part? eg. when upgrading from 7.2 to 7.3

"extra": {
"symfony": {
"allow-contrib": false,
"require": "7.2.*"
}
}

https://redd.it/1mj6r1f
@r_php
Learning Symfony & Twig: Components, Conventions, and IDE Pains

Hi everyone,

I'm currently learning Symfony and Twig, and I’ve run into a few questions regarding best practices for structuring Twig components and improving developer experience. I’d really appreciate your input:



1. Where should I place Twig components ? I already have a folder structure for my templates, so if I need to move everything into a specific folder, that could be inconvenient.
2. What’s the convention for distinguishing between PascalCase and kebab-case? (I get the impression that everything that isn’t a class is written in kebab-case. However, in the case of templates vs TwigComponents, this creates a bit of an awkward inconsistency. So I’m not sure if I can use PascalCase for everything, or if I should keep using kebab-case for templates that aren't Twig components.),
3. Is it forbidden to have files prefixed with an underscore? Do they always have to start with a letter, or sometimes even an uppercase letter?,
4. What’s the naming convention regarding the “s” at the end of file or folder names? For example, sometimes we see "templates" (with an “s”), but for "api", it's usually singular.,
5. I’m working on VS Code, but auto-import doesn’t work, and incorrect paths aren’t being detected either. Yet I’ve installed all the recommended extensions, including PHP Intelephense and PHP Namespace Resolver, and according to various LLMs, my settings.json is correctly configured. Another feature that would be interesting is automatic reference updating when a file is moved. Is that even possible in PHP/Twig?, I’ve seen quite a few similar issues online — some people recommend switching to PhpStorm, which apparently has fewer problems. Would that really be the better solution?
6. Is there a GitHub repository that shows a realistic example of best practices and usage of Twig components?

Thanks in advance for any guidance, tips, or resources. 🙏

https://redd.it/1mjb92n
@r_php
Symfony vs Laravel

The post "Symfony vs Laravel - Understanding the Differences" on kodmatrix.com is an insightful resource for developers who want to compare these two leading PHP frameworks. It highlights key aspects such as learning curve, performance, modularity, scalability, and ease of use.



https://redd.it/1mjfj2z
@r_php
Magicless PHP framework?

First I'd like to say that I have nothing against the modern frameworks full of reflection and other dark magic, but I'm wondering if there's a PHP framework that is rather explicit than implicit in how it works, so that I don't need extra editor plugins to understand things such as type hints or what methods a class has.

Laravel, while great, often feels like programming in a black box. Methods on many of the classes don't exist (unless you use PHPStorm and Laravel Idea, or other extra plugins), data models have magic properties that also don't exist, and so on and so on, which makes me constantly go back and forth between the DB and the code to know that I'm typing a correct magic property that corresponds to the db column, or model attribute, or whatever ... and there's a ton of stuff like this which all adds up to the feeling of not really understanding how anything works, or where anything goes.

I'd prefer explicit design, which perhaps is more verbose, but at least clear in its intent, and immediately obvious even with a regular PHP LSP, and no extra plugins. I was going to write my own little thing for my own projects, but before I go down that path, thought of asking if someone has recommendations for an existing one.

https://redd.it/1mjvdxw
@r_php
How do you handle exceptions which you expect not to be thrown?

This question bugs me for a long time.
Often times an exception could be theoretically thrown but this will never happen in practice because the codepath is basically static.
Thats also wouldnt be a Problem if IDEs like PHPStorm wouldnt warn you about every unhandled exception through the complete stack trace.

So what patterns are you using to handle stuff like `DateMalformedException`, `RandomException` etc. which you no wont throw and if so it should crash.

For example given the following method:

/**
* @return $this
* @noinspection PhpDocMissingThrowsInspection // if removed doccomment also triggers warning
*/
public function expire(): self
{
$this->expirationDate = new DateTime();
$this->expirationDate->modify("-1 day"); // Unhandled \DateMalformedStringException

return $this;
}

Because the values are static you can expect that it works. More dynamic example would be:

function addDays(DateTime $date, int $days): DateTime
{
$date = clone $date;
$date->modify("+$days days"); // Even in this case we can safely assume the format is always correct because days is an int.
return $date;
}

Another candidate is `random_int`

random_int(1, 10); // Unhandled \Random\RandomException

https://redd.it/1mk3xzd
@r_php
Newbie learning to educate myself

Hey everyone!

I'm a bit new to the programming world (besides a few classes in early college years) and was wanting to start my journey into programming with PHP.

Why?
Well, I work closely with the owners of a photography business who commissioned a photo album app with compression and all this other stuff from someone on Upwork but, none of them really know the behind the scenes as it relates to the code.

It's still in development but what they're using is SQL, cloudflare R2, and laravel so I was hoping to get educated enough to at least have some degree of experience with keeping things in line (long, long road from doing anything but, it at least would give us some understanding of what we're looking at besides just being the just app users).

So, my question is, is there a best way to help educate myself on these topics to be somewhat competent and help out once the app is complete? I know it will be a long road, but I'm up for it!

Thanks!

https://redd.it/1mk6ggp
@r_php