Psalm reports InvalidArrayOffset, but the value will be within bounds - php

When running Psalm on this simple "random string" generator:
$letters = 'abcdefghjklmnpqrstuvwxyz';
$numbers = '23456789';
$number_count = \strlen($numbers);
$letter_count = \strlen($letters);
$pass = '';
while (\strlen($pass) < 9) {
$pass .= $letters[\random_int(0, $letter_count - 1)];
}
while (\strlen($pass) < 12) {
$pass .= $numbers[\random_int(0, $number_count - 1)];
}
I get an error (as seen here):
ERROR: InvalidArrayOffset - 15:11 - Cannot access value on variable $numbers using a int offset, expecting -8|-7|-6|-5|-4|-3|-2|-1|0|1|2|3|4|5|6|7
What I don't understand, is what I get the error on line 11, and not 4 lines earlier, when I'm fetching characters from $letters.
I'm doing exactly the same both times, yet on the second one it seems psalm can't infer the possible return values from random_int(), and that they will be within acceptable values. And yet it can the first time.
What I'm doing wrong, and how can I make Psalm understand the code?
I know that the code works, but I am confused why the first $pass assignment does not trigger an error, but the second one does.

The code is fine. The issue described here is tracked at https://github.com/vimeo/psalm/issues/5458.

Related

PHP Human Readable filesize script always returns a "B" [duplicate]

This question already has answers here:
Format bytes to kilobytes, megabytes, gigabytes
(28 answers)
Closed 3 years ago.
After thinking mine was in error,
I found LOT AT LOTS of scripts the do this:
https://gist.github.com/liunian/9338301
And there are several here at S.O. I used, but had the same annoying "B" as a size.
This issue seemed to rear it's ugly head when I switched to php v7.xxx
First issues is I have to typcase a floated number (or double) or else I get a
"A non well formed numeric value encountered"
After some research, apparently this is NOT a bug. At least that is how I read it.
So after typcasting it, the error goes away but the value returned is always a "B'
filesize = 87.5B (when it should be MB or GB).
I am pretty sure Javascript will work, but would rather keep it with php.
Thanks for looking
current live script that is producing a "B" only
public function readableBytes($size, $type='pc') { //ignore the pc - it is for something else - disabled for debugging
$size = (double)$size;
static $units = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$step = 1024;
$i = 0;
while (($size / $step) > 0.9) {
$size = $size / $step;
$i++;
}
return round($size, 2).$units[$i];
}// function readbbleBytes
This is a unique issue to those that use a loop for assigning (custom)template tags with an array field.
The filesize is one of many fields in an array I use.
I used a loop to go through each field and assign it to a template tag.
Not sure why the "B" came up. My suspicion is that the result of a "string" = 0 bytes. Even though It showed the actual size.
edit: spelling & claification
So to fix, in the middle of the loop, I forced the $array['filesize'] = readableBytes($array['filesize'])).
Before it was loop tag = fieldname.
foreach ($arr as $field=>$data) {
$arr['filesize'] = readableBytes($array['filesize'])); // fix was put here
$page = str_ireplace("{$field}", $data, $page);
}
The following seems to work (checked in phptester):
function human_filesize($bytes, $decimals = 2)
{
if ($bytes < 1024) {
return $bytes . ' B';
}
$factor = floor(log($bytes, 1024));
return sprintf("%.{$decimals}f ", $bytes / pow(1024, $factor)) . ['B', 'KB', 'MB', 'GB', 'TB', 'PB'][$factor];
}
This is a cut and paste of a post by gladx in the following thread, where you'll find several optimised examples: https://gist.github.com/liunian/9338301#gistcomment-2183132

How does python converts mysql binary(16)

I have two apps one written in php and one in python and both of them use the same mysql database.
For the public id of the entries in some of the tables I use binary(16) fields(I can't change this, it must remain this way).
The question is how does python does the conversion of this binary field?
Let's take one of the entries as an example.
When I get it in php(from the db) the value of the public id is °•WiCÄ‘õ0Iò|–g, the same value is shown in SequelPro. But php myAdmin does a hex function over binary fields and shows 0bb09557691443c491f53049f27c9667. Now I managed in php to convert the binary to the value showed in php myAdmin and it works for all the entries but I've just noticed that python does another conversion. When I get the entry used in this example via python the public id is owwweye1rjnvt3i1d0ib18x3.
What I need to achieve is to convert in php what I get from MySql: °•WiCÄ‘õ0Iò|–g to what python sees: owwweye1rjnvt3i1d0ib18x3. The php app makes calls on the python one(not developed by me) and thus the id needs to be in the same format for a successfull call.
Any suggestions are welcomed. Thanks.
EDIT: If i send °•WiCÄ‘õ0Iò|–g from php to python and print it rigth away I get: °•WiCÄ‘õ0Iò|–g
Finally I've sorted this out.
Seems that python converts to base36 not hex as I've wrongly supposed.
I've tried to simply base_convert 0bb09557691443c491f53049f27c9667 from 16 to 36 but I've got owwweye1rk04k4cskkw4s08s. Not really what I needed but still a great step further as it started to look like owwweye1rjnvt3i1d0ib18x3.
This difference I supposed to appear because of the large values to be converted(loss of precision), so I've further researched and found the bellow function, written by Clifford dot ct at gmail dot com on the php.net website:
<?php
function str_baseconvert($str, $frombase=10, $tobase=36) {
$str = trim($str);
if (intval($frombase) != 10) {
$len = strlen($str);
$q = 0;
for ($i=0; $i<$len; $i++) {
$r = base_convert($str[$i], $frombase, 10);
$q = bcadd(bcmul($q, $frombase), $r);
}
}
else $q = $str;
if (intval($tobase) != 10) {
$s = '';
while (bccomp($q, '0', 0) > 0) {
$r = intval(bcmod($q, $tobase));
$s = base_convert($r, 10, $tobase) . $s;
$q = bcdiv($q, $tobase, 0);
}
}
else $s = $q;
return $s;
}
?>
I don't think others will come across this issue very often, but still if it happens hope they'll find this instead of burning their brains out like I did :))))

Does a "clamp" number function exist in PHP?

I wrote a function to "clamp" numbers in PHP, but I wonder if this function exists natively in the language.
I read PHP.net documentation in the math section, but I couldn't find it.
Basically what my function does is that it accepts a variable, an array of possible values, and a default value, this is my function's signature:
function clamp_number($value, $possible_values, $default_value)
If $value does not match any of the $possible_values then it defaults to $default_value
I think my function would be way faster if PHP already provides it natively because I'm using quite often in my program.
It seems as though you are just trying to find a number within a set. An actual clamp function will make sure a number is within 2 numbers (a lower bounds and upper bounds). So psudo code would be clamp(55, 1, 10) would produce 10 and clamp(-15, 1, 10) would produce 1 where clamp(7, 1, 10) would produce 7. I know you are looking for more of an in_array method but for those who get here from Google, here is how you can clamp in PHP without making a function (or by making this into a function).
max($min, min($max, $current))
For example:
$min = 1;
$max = 10;
$current = 55;
$clamped = max($min, min($max, $current));
// $clamped is now == 10
A simple clamp method would be:
function clamp($current, $min, $max) {
return max($min, min($max, $current));
}
$value = in_array($value, $possible_values) ? $value : $default_value;
I think is worth to know that...
https://wiki.php.net/rfc/clamp
Is approved and will exist in the core

Generating unique 6 digit code

I'm generating a 6 digit code from the following characters. These will be used to stamp on stickers.
They will be generated in batches of 10k or less (before printing) and I don't envisage there will ever be more than 1-2 million total (probably much less).
After I generate the batches of codes, I'll check the MySQL database of existing codes to ensure there are no duplicates.
// exclude problem chars: B8G6I1l0OQDS5Z2
$characters = 'ACEFHJKMNPRTUVWXY4937';
$string = '';
for ($i = 0; $i < 6; $i++) {
$string .= $characters[rand(0, strlen($characters) - 1)];
}
return $string;
Is this a solid approach to generating the code?
How many possible permutations would there be? (6 Digit code from pool of 21 characters). Sorry math isn't my strong point
21^6 = 85766121 possibilities.
Using a DB and storing used values is bad. If you want to fake randomness you can use the following:
Reduce to 19 possible numbers and make use of the fact that groups of order p^k where p is an odd prime are always cyclic.
Take the group of order 7^19, using a generator co-prime to 7^19 (I'll pick 13^11, you can choose anything not divisible by 7).
Then the following works:
$previous = 0;
function generator($previous)
{
$generator = pow(13,11);
$modulus = pow(7,19); //int might be too small
$possibleChars = "ACEFHJKMNPRTUVWXY49";
$previous = ($previous + $generator) % $modulus;
$output='';
$temp = $previous;
for($i = 0; $i < 6; $i++) {
$output += $possibleChars[$temp % 19];
$temp = $temp / 19;
}
return $output;
}
It will cycle through all possible values and look a little random unless they go digging. An even safer alternative would be multiplicative groups but I forget my math already :(
There is a lot of possible combination with or without repetition so your logic would be sufficient
Collision would be frequent because you are using rand see str_shuffle and randomness.
Change rand to mt_rand
Use fast storage like memcached or redis not MySQL when checking
Total Possibility
21 ^ 6 = 85,766,121
85,766,121 should be ok , To add database to this generation try:
Example
$prifix = "stamp.";
$cache = new Memcache();
$cache->addserver("127.0.0.1");
$stamp = myRand(6);
while($cache->get($prifix . $stamp)) {
$stamp = myRand(6);
}
echo $stamp;
Function Used
function myRand($no, $str = "", $chr = 'ACEFHJKMNPRTUVWXY4937') {
$length = strlen($chr);
while($no --) {
$str .= $chr{mt_rand(0, $length- 1)};
}
return $str;
}
as Baba said generating a string on the fly will result in tons of collisions. the closer you will go to 80 millions already generated ones the harder it will became to get an available string
another solution could be to generate all possible combinations once, and store each of them in the database already, with some boolean column field that marks if a row/token is already used or not
then to get one of them
SELECT * FROM tokens WHERE tokenIsUsed = 0 ORDER BY RAND() LIMIT 0,1
and then mark it as already used
UPDATE tokens SET tokenIsUsed = 1 WHERE token = ...
You would have 21 ^ 6 codes = 85 766 121 ~ 85.8 million codes!
To generate them all (which would take some time), look at the selected answer to this question: algorithm that will take numbers or words and find all possible combinations.
I had the same problem, and I found very impressive open source solution:
http://www.hashids.org/php/
You can take and use it, also it's worth it to look in it's source code to understand what's happening under the hood.
Or... you can encode username+datetime in md5 and save to database, this for sure will generate an unique code ;)

generating an sequential five digit alphanumerical ID

General Overview:
The function below spits out a random ID. I'm using this to provide a confirmation alias to identify a record. However, I've had to check for collision(however unlikely), because we are only using a five digit length. With the allowed characters listed below, it comes out to about 33 million plus combinations. Eventually we will get to five million or so records so collision becomes an issue.
The Problem:
Checking for dupe aliases is inefficient and resource heavy. Five million records is a lot to search through. Especially when this search is being conducted concurrently by different users.
My Question:
Is there a way to 'auto increment' the combinations allowed by this function? Meaning I only have to search for the last record's alias and move on to the next combination?
Acknowledged Limitations:
I realize the code would be vastly different than the function below. I also realize that mysql has an auto increment feature for numerical IDs, but the project is requiring a five digit alias with the allowed characters of '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'. My hands are tied on that issue.
My Current Function:
public function random_id_gen($length)
{
$characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$max = strlen($characters) - 1;
$string = '';
for ($i = 0; $i < $length; $i++) {
$string .= $characters[mt_rand(0, $max)];
}
return $string;
}
Why not just create a unique index on the alias column?
CREATE UNIQUE INDEX uniq_alias ON MyTable(alias);
at which point you can try your insert/update and if it returns an error, generate a new alias and try again.
What you really need to do is convert from base 10 to base strlen($characters).
PHP comes with a built in base_convert function, but it doesn't do exactly what you want as it will use the numbers zero, one and the letter 'o', which you don't have in your version. So you'll need a function to map the values from base_convert from/to your values:
function map_basing($number, $from_characters, $to_characters) {
if ( strlen($from_characters) != strlen($to_characters)) {
// ERROR!
}
$mapped = '';
foreach( $ch in $number ) {
$pos = strpos($from_characters, $ch);
if ( $pos !== false ) {
$mapped .= $to_characters[$pos];
} else {
// ERROR!
}
}
return $mapped;
}
Now that you have that:
public function next_id($last_id)
{
$my_characters = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ';
$std_characters ='0123456789abcdefghijklmnopqrstuv';
// Map from your basing to the standard basing.
$mapped = map_basing($last_id, $my_characters, $std_characters);
// Convert to base 10 integer and increment.
$intval = base_convert($mapped, strlen($my_characters), 10);
$intval++;
// Convert to standard basing, then to our custom basing.
$newval_std = base_convert($intval, 10, strlen($my_characters));
$newval = map_basing($newval_std, $std_characters, $my_characters);
return $newval;
}
Might be some syntax errors in there, but you should get the gist of it.
You could roll your own auto-increment. It would probably be fairly inefficient though as you'd have to figure out where in the process your increment was. For instance, if you assigned the position in your random string as an integer and started with (0)(0)(0)(0)(0) that would equate to 22222 as the ID. Then to get the next one, just increment the last value to (0)(0)(0)(0)(1) which would translate into 22223. If the last one gets to your string length, then make it 0 and increment the second to last, etc... It's not exactly random, but it would be incremented and unique.

Categories