PHP Reddit – Telegram
PHP Reddit
34 subscribers
292 photos
37 videos
24.9K links
Channel to sync with /r/PHP /r/Laravel /r/Symfony. Powered by awesome @r_channels and @reddit2telegram
Download Telegram
I made a tiny PHPUnit extension for PSR-7, XML/HTML and JSON assertions

Hi! I found myself often struggling with testing against server PSR-7 responses, XML and JSON documents, Frameworks like Laravel and Symfony offer useful assertions for that but it's often that a project is not using either of the frameworks or it's just not enough. So I made an extension to PHPUnit:

https://github.com/stein197/phpunit-extended

I'd be glad if someone finds its also useful or finds any issues with it

https://redd.it/1k2zy0l
@r_php
PHP named argument unpacking

Hello, I am planning to make an RFC but before that I wanted to know your opinion on it.

Currently PHP allows named arguments so we can do something like this:

function calc(int $number, int $power): int

calc(number: $number, power: $power);

But when we have a variable parameter.

function calc(int $number, int ...$power): int

It is not possible to call the function using named parameters since PHP does not know how to group variable parameters so this is my proposal:

calc(number: $number, power: { $calc_number_1, $calc_number_2, $calc_number_3 });

{ } could be used only when trying to call a function with named parameters and one of its parameters is variadic.

A simpler example to visualize would be this:

function calc(array $into, array ...$insert): array

calc(insert: { $var_1, $var_2, $var_3 }, into: $my_array);

This syntax is much clearer and easier to understand than:

calc(insert: [$var_1, $var_2, $var_3], into: $my_array);

Also, another point to take into account is that with this new syntax, you could combine "parameters by reference" with "named parameters"
For example:

function build_user(int $type, mixed &...$users) {
foreach($users as &$user) {
// Any actions
$user = new my\User(...);
}

build_user(type: $user_type, users: { $alice, $dominic, $patrick });

$alice->name('Alice');

$dominic->name('dominic');

$patrick->name('patrick');

-------------------------------------------------------------------------------

This is already an extra:

If when using named parameters together with the variadic variable, the unpacker returns as a key the name of the sent variable you could do something like this:

function extract(string $path, mixed &...$sources) {
foreach( $sources as $type => &$source) {
switch($type) {
case: 'css':
$source = new my\CSS($path);
break;
case: 'js':
$source = new my\JS($path);
default:
$source = null;
}
}

extract(path: $my_path, sources: { $css, $js });

print $css;

print $js;

Another extra:

It would also be possible to have more than one variadic parameter in a function.

function zoo(int $id, Mammals ...$mammals, ...Cetaseans $cetaceans, Birds ...$birds)

zoo(id: $id, mammals: { $elephants, $giraffes, $wolves }, cetaseans: { $belugas }, birds: { $eagles, $hummingbirds });

https://redd.it/1k37pfd
@r_php
How much overhead does DDEV take when the applications are in operation?

When the web, database and other service related containers setup for docker by DDEV are in operation do the requests have to be proxied through some DDEV services running in the background?

I take it that with some DDEV services listening on port 80 and 443 on the Docker host there may be some overhead, but does that entail some real computational work?

I just want to ascertain that other than issuing the ddev commands to the docker containers DDEV doesn't incur much overhead, and that any overhead will be down to the containers themselves.

https://redd.it/1k37i3x
@r_php
One month into PHP and I feel like I’m getting nowhere. Is this normal ?

I’ve just started learning PHP from scratch it’s been almost a month now. But honestly, I’m finding it super boring. Feels like I’m not getting anywhere. I study, but nothing sticks… I keep forgetting everything. Does this happen to everyone, or is it just me?

https://redd.it/1k3j7m6
@r_php
Weekly /r/Laravel Help Thread

Ask your Laravel help questions here. To improve your chances of getting an answer from the community, here are some tips:

What steps have you taken so far?
What have you tried from the documentation?
Did you provide any error messages you are getting?
Are you able to provide instructions to replicate the issue?
Did you provide a code example?
Please don't post a screenshot of your code. Use the code block in the Reddit text editor and ensure it's formatted correctly.

For more immediate support, you can ask in the official Laravel Discord.

Thanks and welcome to the r/Laravel community!

https://redd.it/1k3r4oz
@r_php
Got an unexpected Laravel Cloud bill :/
https://redd.it/1k414p0
@r_php
Weekly Ask Anything Thread

Feel free to ask any questions you think may not warrant a post. Asking for help here is also fine.

https://redd.it/1k43szz
@r_php
Weekly help thread

Hey there!

This subreddit isn't meant for help threads, though there's one exception to the rule: in this thread you can ask anything you want PHP related, someone will probably be able to help you out!

https://redd.it/1k46h74
@r_php
I want to optimze this code, Now it just pending, not getting rendered html

public function getFilterData(Request $request)
    {
        try {
            $sbu = $request->sbu;
            $location = $request->location;
            $customer = $request->customer;
            $salesRep = $request->salesRep;
            $asAt = $request->asAt;
    
            $html = ''; //html table
    
            //common style
            $styles = [
                'borderTop' => 'border-top: 2px solid #000;',
                'borderBottom' => 'border-bottom: 1px solid #000; border-bottom: 3px double #000;',
                'textAlign' => 'text-align: right;',
            ];
    
            $count = 1; //count
    
            $data = $this->requiredData($sbu, $location, $customer, $salesRep, $asAt, $request);
    
            //grouped data
            $groupedCollection = $data->groupBy('cti');
            
            //all sum
            $totalAmountSumAll = 0;
            $paymentSumAll = 0;
            $balanceSumAll = 0;
            $oneOverDueSumAll = 0;
            $twoOverDueSumAll = 0;
            $threeOverDueSumAll = 0;
            $fourOverDueSumAll = 0;
            $fiveOverDueSumAll = 0;
            $sixOverDueSumAll = 0;
            $lateFeesSumAll = 0;
            $totalDueSumAll = 0;
    
            //set table data
            $callingCod = '';
            if ($groupedCollection != null) {
                foreach ($groupedCollection as $key => $group) {
                    $customerCode = isset($group[0]['cusCode']) ? $group[0]['cusCode'] : null;
                    $customerName = isset($group[0]['cusName']) ? $group[0]['cusName'] : null;
                    $callingCode = isset($group[0]['cCodeGMoNo']) ? $group[0]['cCodeGMoNo'] : null;
                    $mobileNo = isset($group[0]['gBNo']) ? $group[0]['gBNo'] : null;
                    //set calling code
                    $cCode = $this->callingCode($callingCode);
                    if (!empty($cCode) && isset($cCode[0]['callingCode'])) {
                        $callingCod = $cCode[0]['callingCode'];
                    }
                    $html .= '<tr>
                        <td></td>
                        <td colspan="4"><strong>' . $customerCode . ' : ' . $customerName . (!empty($mobileNo) ? ' | +' . $callingCod . ' ' . $mobileNo : '') .  '</strong></td>
                    </tr>';
    
                    //customer wise sum
                    $totalAmountSum = 0;
                    $paymentSum = 0;
                    $balanceSum = 0;
                    $oneOverDueSum = 0;
                    $twoOverDueSum = 0;
                    $threeOverDueSum = 0;
                    $fourOverDueSum = 0;
                    $fiveOverDueSum = 0;
                    $sixOverDueSum = 0;
                    $lateFeesSum = 0;
                    $totalDueSum = 0;
    
                    // Apply chunking to the group processing - chunk size of 100 can be adjusted
                    $chunkSize = 100;
                    $chunks = array_chunk($group->toArray(), $chunkSize);
                    
                    foreach ($chunks as $chunk) {
                        // Process each chunk of items
                        foreach ($chunk as $item) {
                            //get date difference 
                            $asAtDate = Carbon::parse($asAt);
                            $effectiveDate = !empty($item['invoiceDate']) ? $item['invoiceDate'] : (!empty($item['cicDate']) ? $item['cicDate'] : (!empty($item['odInvoiceDate']) ? $item['odInvoiceDate'] : null));
                            $effectiveDate = Carbon::parse($effectiveDate);
                            $overDueVal = $asAtDate->diffInDays($effectiveDate);
    
                            //over due
                            $oneOverDue = 0;
                            $twoOverDue = 0;
                            $threeOverDue = 0;
                            $fourOverDue = 0;
                            $fiveOverDue = 0;
                            $sixOverDue = 0;
    
                            if ($overDueVal >= 1 && $overDueVal <= 7) {
                                $oneOverDue = $item['amount'] - $item['paymnt'];
                            } else if ($overDueVal >= 8 && $overDueVal <= 14) {
                                $twoOverDue = $item['amount'] - $item['paymnt'];
                            } else if ($overDueVal >= 15 && $overDueVal <= 30) {
                                $threeOverDue = $item['amount'] - $item['paymnt'];
                            } else if ($overDueVal >= 31 && $overDueVal <= 60) {
                                $fourOverDue = $item['amount'] - $item['paymnt'];
                            } else if ($overDueVal >= 61 && $overDueVal <= 90) {
                                $fiveOverDue = $item['amount'] - $item['paymnt'];
                            } else if ($overDueVal > 91) {
                                $sixOverDue = $item['amount'] - $item['paymnt'];
                            }
    
                            $balance = $item['amount'] - $item['paymnt']; //balance
                            $lateFee = $item['lateFee'] - $item['stlmntDiscnt'];
                            $totalDue = $balance + $lateFee; //total due
    
                            $html .= '<tr>
                                <td>' . $count++ . '</td>
                                <td>' . ($item['tty'] == 1 ? 'INV' : ($item['tty'] == 2 ? 'CHQ RTN' : ($item['tty'] == 3 ? 'CRD NO' : ($item['tty'] == 4 ? 'OTH CRD' : '')))) . '</td>
                                <td>' . (isset($item['invoiceNo']) ? $item['invoiceNo'] : (isset($item['inhandChequeNo']) ? $item['inhandChequeNo'] : (isset($item['oldInvoiceNo']) ? $item['oldInvoiceNo'] : ''))) . '</td>
                                <td>' . (!empty($item['invoiceDate']) ? $item['invoiceDate'] : (!empty($item['cicDate']) ? $item['cicDate'] : (!empty($item['odInvoiceDate']) ? $item['odInvoiceDate'] : '-'))) . '</td>
                                <td>' . (isset($item['invoiceNo']) && $item['invMrf'] !== null ? $item['invMrf'] : '-') . '</td>
                                <td style="text-align: right">' . (isset($item['creditPeriodDays']) ? $item['creditPeriodDays'] : 0) . ' Days' . '</td>   
                                <td style="text-align: right">' . $item['remngDays'] . ' Days' . '</td>
                                <td style="text-align: right">' . ($item['amount'] !== 0.0 && $item['amount'] !== 0 ? number_format($item['amount'], 2) : '-') . '</td>
                                <td style="text-align: right">' . ($item['paymnt'] !== 0.0 && $item['paymnt'] !== 0 ? number_format($item['paymnt'], 2) : '-') . '</td>
                                <td style="text-align: right">' . number_format($item['amount']  - $item['paymnt'], 2) . '</td>
                                <td style="text-align: right">' . ($oneOverDue !== 0 && $oneOverDue !== 0.0 ? number_format($oneOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($twoOverDue !== 0 && $twoOverDue !== 0.0 ? number_format($twoOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($threeOverDue !== 0 && $threeOverDue !== 0.0 ?  number_format($threeOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($fourOverDue !== 0 && $fourOverDue !== 0.0 ? number_format($fourOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($fiveOverDue !== 0 && $fiveOverDue !== 0.0 ? number_format($fiveOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($sixOverDue !== 0 && $sixOverDue !== 0.0 ? number_format($sixOverDue, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($lateFee !== 0.0 && $lateFee !== 0 ? number_format($lateFee, 2) : '-') . '</td>
                                <td style="text-align: right">' . ($totalDue !== 0.0 && $totalDue !== 0 ? number_format($totalDue, 2) : '-') . '</td>
                            </tr>';
    
                            $totalAmountSum += $item['amount'];
                            $paymentSum += $item['paymnt'];
                            $balanceSum += $balance;
                            $oneOverDueSum += $oneOverDue;
                            $twoOverDueSum += $twoOverDue;
                            $threeOverDueSum += $threeOverDue;
                            $fourOverDueSum += $fourOverDue;
                            $fiveOverDueSum += $fiveOverDue;
                            $sixOverDueSum += $sixOverDue;
                            $lateFeesSum += $lateFee;
                            $totalDueSum += $totalDue;
                        }
                    }
    
                    //set customer wise sum
                    $html .= '<tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($totalAmountSum !== 0.0 && $totalAmountSum !== 0 ? number_format($totalAmountSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($paymentSum !== 0.0 && $paymentSum !== 0 ? number_format($paymentSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($balanceSum !== 0.0 && $balanceSum !== 0 ? number_format($balanceSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($oneOverDueSum !== 0.0 && $oneOverDueSum !== 0 ? number_format($oneOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($twoOverDueSum !== 0.0 && $twoOverDueSum !== 0 ? number_format($twoOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($threeOverDueSum !== 0.0 && $threeOverDueSum !== 0 ? number_format($threeOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($fourOverDueSum !== 0.0 && $fourOverDueSum !== 0 ? number_format($fourOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($fiveOverDueSum !== 0.0 && $fiveOverDueSum !== 0 ? number_format($fiveOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($sixOverDueSum !== 0.0 && $sixOverDueSum !== 0 ? number_format($sixOverDueSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($lateFeesSum !== 0.0 && $lateFeesSum !== 0 ? number_format($lateFeesSum, 2) : '-') . '</td>
                        <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($totalDueSum !== 0.0 && $totalDueSum !== 0 ? number_format($totalDueSum, 2) : '-') . '</td>
                    </tr>';
    
                    $totalAmountSumAll += $totalAmountSum;
                    $paymentSumAll += $paymentSum;
                    $balanceSumAll += $balanceSum;
                    $oneOverDueSumAll += $oneOverDueSum;
                    $twoOverDueSumAll += $twoOverDueSum;
                    $threeOverDueSumAll += $threeOverDueSum;
                    $fourOverDueSumAll += $fourOverDueSum;
                    $fiveOverDueSumAll += $fiveOverDueSum;
                    $sixOverDueSumAll += $sixOverDueSum;
                    $lateFeesSumAll += $lateFeesSum;
                    $totalDueSumAll += $totalDueSum;
                }
    
                // Rest of your code for total sum rows remains unchanged
                $html .= '<tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>';
    
                $html .= '<tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($totalAmountSumAll !== 0 && $totalAmountSumAll !== 0.0 ? number_format($totalAmountSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($paymentSumAll !== 0 && $paymentSumAll !== 0.0 ? number_format($paymentSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($balanceSumAll !== 0 && $balanceSumAll !== 0.0 ? number_format($balanceSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($oneOverDueSumAll !== 0 && $oneOverDueSumAll !== 0.0 ? number_format($oneOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($twoOverDueSumAll !== 0 && $twoOverDueSumAll !== 0.0 ? number_format($twoOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($threeOverDueSumAll !== 0 && $threeOverDueSumAll !== 0.0 ? number_format($threeOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($fourOverDueSumAll !== 0 && $fourOverDueSumAll !== 0.0 ? number_format($fourOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($fiveOverDueSumAll !== 0 && $fiveOverDueSumAll !== 0.0 ? number_format($fiveOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($sixOverDueSumAll !== 0 && $sixOverDueSumAll !== 0.0 ? number_format($sixOverDueSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($lateFeesSumAll !== 0 && $lateFeesSumAll !== 0.0 ? number_format($lateFeesSumAll, 2) : '-') . '</td>
                    <td style="' . $styles['borderTop'] . ' ' . $styles['borderBottom'] . ' ' . $styles['textAlign'] . '">' . ($totalDueSumAll !== 0 && $totalDueSumAll !== 0.0 ? number_format($totalDueSumAll, 2) : '-') . '</td>
                </tr>';
            } else {
                $html .= '<tr>
                    <td colspan="18">
                        <div role="alert" class="alert alert-warning alert-dismissible fade show" style="text-align: center;">
                           
                            ' . __("finance/reports/debtor_aging_details.data_empty_massage") . '
                        </div>
                    </td>
                </tr>';
            }
    
            return response()->json(['stts' => 1, 'data' => $html]);
        } catch (Exception $e) {
            dd($e);
            $randomErrorCode = errorLog(authrz_debtorAgingDetails("transaction"), $request, $e, $this->datetime);
            return response()->json(['stts' => 0, "mssg" => __('message.danger'), "rfms" => __('message.dangerRef', ['refr' => $randomErrorCode]), "burl" => '#']);
        }
    }


I want to optimize above function and can't affect to its calculation and ui  

https://redd.it/1k488xh
@r_php
Backup laravel mail Sender

Is there a way to have a bacup mail Sender, example: my main sender returned some kind of error, so the app tries to send in another one.

I did some research and find nothing about it.

https://redd.it/1k4efo7
@r_php
Laravel but static?

A while back I did this small static site using jigsaw plus some simple form handling in php. It worked fine, wasn't my favorite to work with, and I kept thinking I should just have just gone with insert framework here behind cloudflare with some aggressive caching... But something something sunk cost lets just keep going.

Fast forward maybe 6 months, and I see the static pages feature in tempest and I have a "yeah that makes perfect sense, why didn't I think of that" moment.

So since I already have a bunch of blade templates for this site I decided to see what it would take to get static pages in laravel. Simpler than I expected...

https://github.com/ssnepenthe/mostly-static

It's a bit rough around the edges and I doubt I will spend much time improving it/cleaning it up. But I thought I would share in case anyone else finds it useful as a starting point for doing something similar.

https://redd.it/1k4tnzl
@r_php
Have very small PHP/MySQL/HTML project, not sure best place to post it

I've looked at /phpjobs and /forhire and not sure the best place to post a request. I see some other websites, like Upwork, as well. But my project is pretty small and self-contained and I want to find a reliable freelancer.


Short version is that I have a website, six pages, with some read-only MySQL calls. I programmed it myself back in 2008, and hired a designer for the look and feel. I have work to do on the website (broken links, old information), but before I do anything I need to bring the PHP up from 7.3 to current 8.4. I haven't programmed anything in years and don't have the head for it anymore (I manage teams in a mainframe iSeries environment). So I need someone to review the code and update any decremented commands, and there are two widgets that no longer work, which probably have common solutions.


So this is a small project, maybe 10-15 hours, with no follow-up projects. I will post more information in a real "for hire" post, but right now not sure where is best to post it to get the right freelancer.

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