[Challenge] Format ListFiles to phpBB3 Coding Guidelines

phpBB3 and MOD challenges setup by the staff to test and challenge your phpBB3 coding skills.

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Handyman » 06 Apr 2007, 11:50

eviL3 wrote:Here you are:

Spoiler:
Code: Select all
/**
 * List files
 *
 * @param string $dir
 * @param bool $recursive
 * @param bool $object
 * @return mixed File list
 */
function list_files ($dir ''$recursive true$object true)
{
    
// Ckeck if the $dir is empty
    
if (empty($dir))
    {
        
$this->push_error('No location has been provided for scaning!');
        exit;
    }
    
    
// Make sure $dir is a "real path"
    
$dir realpath($dir);
    
    
// Make sure $list is set
    
$list = array();
    
    
// Loop through the files
    
$handle opendir($dir);
    while (
false !== ($file readdir($handle)))
    {
        if (!
in_array($file, array('.''..')))
        {
            
$list[$file] = ($object) ? new file_info(realpath($dir '/' $filename), $recursive$object) : $this->info(realpath($dir '/' $filename), $recursive$object);
        }
    }
    
closedir($handle);
    
    
ksort($list);
    
    return 
$list;
}


well? after a quick glance, I only see one problem? overall? it looks really good? but what do I expect Tongue
Code: Select all
$this->push_error('No location has been provided for scaning!');

Language should never be put directly in a php file? it should always be put in a language file.
And, since this is inside a function, you would use
Code: Select all
trigger_error($user->lang['NO_LOCATION_PROVIDED']);

you would also have to add $user as a global.

If it was not inside a function, you would just use
Code: Select all
trigger_error('NO_LOCATION_PROVIDED');

since the language variables would be included in the page.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male


Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby eviL3 » 07 Apr 2007, 04:13

That confuses me. Okay, using language variables is correct (it wasn't clearly stated that it should be used in phpBB, so i just cleaned it up). But why do you use $user->lang in functions? Makes no sense to me. have a look in includes/functions.php at the function msg_handler(). $user is globaled there, and if $errno is E_USER_WARNING or E_USER_NOTICE, we have:
Code: Select all
            if (empty($user->lang))
            {
                
$user->setup();
            }

            
$msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text;
            
$msg_title = (!isset($msg_title)) ? $user->lang['INFORMATION'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title);


I think using $user->lang has nothing to do with it being inside a function (as that's not how variable scope works), but with the error level of the triggered error. A critical error, such as E_USER_ERROR would require using $user->lang, because it's not automatically converted.

Feel free to correct me, if i'm wrong Smile
Image
User avatar
eviL3    
MOD Author
MOD Author
 
Posts: 1002
Joined: 05 Nov 2006, 08:14
Location: Cooking in the MODs kitchen
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby silencer0 » 07 Apr 2007, 06:09

Shock wow lol coding talk xD i must be in really low level for not to understand a thing of what you told lol at least i'm trying Tongue

i'm bad Bang Head Cray lol
Proud to be working on : - Extended Warning Mod Grin
silencer0
Ensign
Ensign
 
Posts: 84
Joined: 10 Feb 2007, 05:08
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby eviL3 » 07 Apr 2007, 06:31

Silencer, perhaps this helps you understand it better: http://php.net/variables.scope
Image
User avatar
eviL3    
MOD Author
MOD Author
 
Posts: 1002
Joined: 05 Nov 2006, 08:14
Location: Cooking in the MODs kitchen
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby A_Jelly_Doughnut » 07 Apr 2007, 09:38

TTBOMK, trigger_error('LANG_STRING'); is always good enough. I've never had issues with this...
A_Jelly_Doughnut
phpBB Team Member
phpBB Team Member
 
Posts: 543
Joined: 10 Feb 2007, 14:58
Location: 1- 800 - In - The - USA
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Handyman » 07 Apr 2007, 12:27

eviL3 wrote:That confuses me. Okay, using language variables is correct (it wasn't clearly stated that it should be used in phpBB, so i just cleaned it up). But why do you use $user->lang in functions? Makes no sense to me. have a look in includes/functions.php at the function msg_handler(). $user is globaled there, and if $errno is E_USER_WARNING or E_USER_NOTICE, we have:
Code: Select all
            if (empty($user->lang))
            {
                
$user->setup();
            }

            
$msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text;
            
$msg_title = (!isset($msg_title)) ? $user->lang['INFORMATION'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title);


I think using $user->lang has nothing to do with it being inside a function (as that's not how variable scope works), but with the error level of the triggered error. A critical error, such as E_USER_ERROR would require using $user->lang, because it's not automatically converted.

Feel free to correct me, if i'm wrong Smile


Handyman wrote:you would also have to add $user as a global.
Smile
Code: Select all
global $user;

then you can use $user->lang anytime in that ufnction.
using the
Code: Select all
$user->lang['INFORMATION']
is the same as using
Code: Select all
{L_INFORMATION}
in templates.
Please contact me if you have any news to submit to SCOFF News.
SCOFFing at the candidates while you sleep.
My Mods || My Mod Queue
Image
User avatar
Handyman    
Rear Fleet Admiral
Rear Fleet Admiral
 
Posts: 7459
Joined: 08 May 2006, 04:45
Location: Where no man has gone before!
Favorite Team: Seattle Seahawks
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby silencer0 » 08 Apr 2007, 05:54

eviL3 wrote:Silencer, perhaps this helps you understand it better: http://php.net/variables.scope

thx now i understand it better Tongue Yahoo lol
Proud to be working on : - Extended Warning Mod Grin
silencer0
Ensign
Ensign
 
Posts: 84
Joined: 10 Feb 2007, 05:08
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Mighty Gorgon » 15 Sep 2007, 05:38

Nice challenge this one. :D

I sometimes use an automated script to automatically adjust code syntax.

Here is the link:
http://www.icyphoenix.com/_beautify/beautify.php

I slightly modified it to suit my needs... the code isn't perfect yet, but let you reformat bad written code easily.

If someone would like to help me in making it compliant with phpBB 3 requirements, I think it could be a great help for coder.

Of course I will share the code if you are interested in.
Luca
User avatar
Mighty Gorgon    
Style Author
Style Author
 
Posts: 20
Joined: 04 Jul 2007, 16:50
Location: Italy
Favorite Team: Juventus
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby john010117 » 13 Dec 2007, 23:27

I believe this is right. :)

Code: Select all
<?php
/**
* List files
*
* @param string $dir
* @param bool $recursive
* @param bool $object
* @return mixed File list
*/
function list_files ($dir = '', $recursive = true, $object = true)
{
   global $user;
   
    // Ckeck if the $dir is empty
    if (empty($dir))
    {
        $this->push_error($user->lang['NO_LOCATION_PROVIDED']);
        exit;
    }
   
    // Make sure $dir is a "real path"
    $dir = realpath($dir);
   
    // Make sure $list is set
    $list = array();
   
    // Loop through the files
    $handle = opendir($dir);
   
    while (false !== ($file = readdir($handle)))
    {
        if (!in_array($file, array('.', '..')))
        {
            $list[$file] = ($object) ? new file_info(realpath($dir . '/' . $filename), $recursive, $object) : $this->info(realpath($dir . '/' . $filename), $recursive, $object);
        }
    }
    closedir($handle);
   
    ksort($list);
   
    return $list;
}
?>
john010117    
Crewman
Crewman
 
Posts: 5
Joined: 10 Jan 2007, 17:29
Gender: Male
phpBB Knowledge: 6

Previous

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 1 guest