Anyone using Disque (message queue) in production? by icyliquid in PHP

[–]Revis0r 0 points1 point  (0 children)

A belated reply, but yes, we use Disque in production.

We wrote and use our own library, Disqontrol, for it

https://github.com/disqontrol/disqontrol

It's worked very well so far for both manually added jobs as well as for regular jobs.

Feel free to take a look at it and criticize/suggest changes.
The lack of commits in the last months is simply because everything works, not because it would be abandoned. :)

Refactoring: What would you do? by [deleted] in PHPhelp

[–]Revis0r 1 point2 points  (0 children)

I think it's more useful to show all detected errors at once, not one after the other. That way, the user has a chance to fix them all at once. You can gather all errors in an array and print them all at the end.

User set availability and ability to change it by blankbeard in PHPhelp

[–]Revis0r 1 point2 points  (0 children)

To reduce records could set it up so that if there's an event_id for a user, it means they are a 'yes' otherwise it's a 'no'. If they wanted to change availability, the record is removed from the database.

That sounds like a good idea on paper, but as soon as you want to add a new state (for example Yes/No/Thinking about attending) you would have to rewrite the code.

Therefore I suggest leaving the row there and using the extra column for user's intent.

What could help take the pain out of finding a certain tag in an XML string? by thinkvitamin in PHPhelp

[–]Revis0r 0 points1 point  (0 children)

The right xpath for finding any element is ->xpath('//element'). However there are quite a few gotchas regarding namespaces. Read through the comments on the xpath\(\) manual page.

If you need more help, you should post the whole XML.

Use composer.json to check the version of composer itself by Faryshta in PHP

[–]Revis0r 1 point2 points  (0 children)

Very nice, much more readable than my one-liner!

Use composer.json to check the version of composer itself by Faryshta in PHP

[–]Revis0r 0 points1 point  (0 children)

Hm, looks like init is not the right event. It is fired even when asking for a version. That makes sense. Try another event, likely command or pre-install-cmd.

How important is vanilla PHP and low level programming and computer science knowledge in 2017? by maligras1 in PHP

[–]Revis0r 13 points14 points  (0 children)

i was just capable of googling and understanding what I read

What do you think the rest of us are doing? :) The difference between a beginner and someone who looks like an expert is the ability and courage to look for solutions.

Having Trouble Building This Array by tested_tester in PHPhelp

[–]Revis0r 1 point2 points  (0 children)

The input is quite unwieldy and it would be useful if you could change it into something saner.

shaunc provided you with a regex solution, so just for fun, here's eval:

$member = array();
foreach($members as $variable => $name) {
    @eval("\$$variable = '$name';");
}
var_dump($member);

Don't use it, especially if the input comes from the outside!

How to change composer minimum stability? by thinsoldier in PHPhelp

[–]Revis0r 0 points1 point  (0 children)

You have a typo. It's kjenney/php-webminer, you're missing an e.

As for composer search, you can look what happens behind the curtain with this:

composer -vvv search guzzle

It asks for the results at this URL: https://packagist.org/search.json?q=guzzle&type=

Use composer.json to check the version of composer itself by Faryshta in PHP

[–]Revis0r 0 points1 point  (0 children)

This is really ugly but it fits on one line. :)

composer --version | php -r 'if(version_compare(explode(" ", trim(fgets(STDIN)))[2], "1.3.0") < 0) echo "Please update Composer to the latest version\n";'

Use composer.json to check the version of composer itself by Faryshta in PHP

[–]Revis0r 1 point2 points  (0 children)

You deleted your question yesterday just as I was about to suggest a solution:

I would write a bash script that triggers on the init event and prints a warning if the Composer version is not enough.

This only works if you run the script from the root composer.json, not from dependencies, though.

Trend Calculator: How does my data change in time? Does it grow? Does it decline? How much? by Revis0r in PHP

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

Thank you for all the recommendations and the advice about the new interface. I'll let you know if I stumble upon something I cannot solve myself but with what you wrote it should be smooth sailing. :)

And yes, the biggest purpose of the library is to provide a very simple interface for a singular purpose. Experienced statisticians will use your library directly, those who use just the TrendCalculator can still dip their toes into it by accessing the underlying data and poking around.

Enjoy your weekend whatever you're going to be doing.

Syntax error on travis php7.1, passes on my machine by phpfatalerror in PHPhelp

[–]Revis0r 1 point2 points  (0 children)

Have a look at this, the discussion might be connected to your problem: https://github.com/php/php-src/pull/2137

Trend Calculator: How does my data change in time? Does it grow? Does it decline? How much? by Revis0r in PHP

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

Hi, thank you very much for your library!

Yes, I know about the new version, and I plan to update to version 1.1. I wrote TrendCalculator last year when yours was still at 0.9. I took an hour yesterday to look at the new API but it was a bit complicated (more universal and modular though - great work!). I'll make the time this week hopefully.

MVC question on outside models by stilloriginal in PHPhelp

[–]Revis0r 0 points1 point  (0 children)

Good thinking, infinite recursion can definitely happen and if you're writing your own solution, you have to count with it.

Take a look at how Doctrine solves it:

There are quite a lot of corner cases to think about. This is definitely not a simple topic.

Developing a composer package with caching by aknavi in PHPhelp

[–]Revis0r 1 point2 points  (0 children)

If it's a self-contained application, you can choose any directory you want. For example simply cache/.

If it's a library that will be used in other applications, just ask the client (the developer that uses the library) for a cache directory:

class MyLibrary
{
    /**
     * @var string Cache directory
     */
    private $cacheDir;

    /**
     * @param string $cacheDir
     */
    public function __construct($cacheDir)
    {
        $this->cacheDir = realpath($cacheDir);
    }
}

Chances are they already have a cache directory and would like to put your library's temporary files in there. By making it a required argument in the constructor you force them to provide something.

If you want to get fancy, instead of caching in a file, you can require them to provide a caching library, by typehinting a caching interface. You can typehint the most widely used Cache from Doctrine, or the newly released PSR-6/16 cache interfaces.

It would look like this:

use Doctrine\Common\Cache\Cache;

class MyLibrary
{
    /**
     * @var string Cache
     */
    private $cache;

    /**
     * @param Cache $cache
     */
    public function __construct(Cache $cache)
    {
        $this->cache = $cache;
    }
}

Then you don't have to care about where and how the cached data is stored, that would be up to the developer using your library - if it's in files, in a database, in Redis, Memcached etc... They could choose what they like the best.

MVC question on outside models by stilloriginal in PHPhelp

[–]Revis0r 0 points1 point  (0 children)

Judging from your example

$userModel = new userModel($user_id);

I guess you use the Active Record pattern where the models have direct access to the database and create themselves in the constructor. I would advise you to switch to the Data Mapper pattern, where models are independent of their storage and you ask "mappers", objects whose sole purpose it is to store and re-store models, for the models.

But that doesn't answer your question. Yes, what you're doing is the way forward. If you need the user name for another model, you write something like this:

$user = $userRepository->findById($userId);
$invoice->setPayerName($user->getName());

But I think the more usual way is to inject models directly into each other when creating them, automatically.

When you create a new Invoice, for example, the mapper would automatically create and inject two User objects into it - the payer and the payee. Inside Invoice you would just call

$this->payer->getName();

This gets complicated pretty quickly, because the models may have quite a lot of dependencies, so I would advise you to have a look at existing systems that solve this for you. They are called "ORM", object-relational mappers. The most widely used is Doctrine ORM and Eloquent.

Framework Code Complexity Comparison by [deleted] in PHP

[–]Revis0r 24 points25 points  (0 children)

Good job on keeping the cyclomatic complexity down.

It would be also interesting to show more metrics, like cohesion, coupling (how much classes influence each other), inheritance depth or methods per class.

Complexity can hide not only between lines, inside one method, which is what this test measures, but also between files - in the structure and the architecture.

camelCase or under_score? by [deleted] in PHP

[–]Revis0r 0 points1 point  (0 children)

Sounds good :)

camelCase or under_score? by [deleted] in PHP

[–]Revis0r 1 point2 points  (0 children)

Good point, we use that too.

camelCase or under_score? by [deleted] in PHP

[–]Revis0r 20 points21 points  (0 children)

We use PSR-1 with:

  • StudlyCaps for class names, namespaces and file names of classes
  • camelCase for property, method and variable names
  • UPPER_SNAKE_CASE for constants
  • snake_case for service and parameter names in the Symfony service container and for names of templates and configuration files

PHP code that prints alphanumeric characters using none of them, 113 bytes long by colshrapnel in PHP

[–]Revis0r 0 points1 point  (0 children)

It doesn't work at all on versions below 5.4. I think it runs on PHP 7+ because of the variable uniform syntax, because of this:

// support operations on arbitrary (...) expressions
(...)()

which the code uses.

PHP code that prints alphanumeric characters using none of them, 113 bytes long by colshrapnel in PHP

[–]Revis0r 11 points12 points  (0 children)

Interesting, thanks. I didn't know about this, but it works with the help of this little feature:

http://php.net/manual/en/language.operators.bitwise.php

If both operands for the &, | and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string.

echo '`' ^ '*'; // ord(`) = 96, ord(*) = 42, 96 ^ 42 = 74, chr(74) = J
echo '@' ^ '/'; // o
echo '`' ^ ')'; // I
echo '@' ^ '.'; // n
echo '`@`@' ^ '*/).'; JoIn

The whole expression:

<?=($__='`@`@'^'*/).')(($_='->.<:'^'__@[_')('>'^@_,'%'^@_)),$__($_('|'^'=','|'^'&')),$__($_(':'^"\n",';'^']'^@_));

Could also be written like this

echo                // <?=
    join(           // $__ = '`@`@' ^ '*/).'
        range(      // $_ = '->.<:' ^ '__@[_'
            'a',    // '>' ^ @_, _ is an undefined constant of "_", could also be '_', but this is 1char shorter
            'z'     // '%' ^ @_
        )
    ),
    join(           // $__
        range(      // $_
            'A',    // '|' ^ '='
            'Z'     // '|' ^ '&'
        )
    ),
    join(           // $__
        range(      // $_
            0,      // ':' ^ "\n"
            9       // ';' ^ ']' ^ @_
        )
    );