[Challenge] Format ListFiles to phpBB3 Coding Guidelines

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

[Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Highway of Life » 01 Mar 2007, 12:52

This is a very simple challenge, basically, your goal is to reformat this piece of code to adhere to phpBB3 coding guidelines.
There will be a total of four or five snippets of code, but I'm only going to release them one at a time until each snippet is properly formatted. -- i.e. because one could "cheat" by duplicating the code of someone who posted it successfully, and I have not install the LOCK / HIDE MOD yet, so we have to do it like this for the moment.

Once all five codes are formatted, I will show this script/MOD in it's entirety, but no sooner. Smile

Here is the first one, once someone completes and posts the formatted version, the second code will be added.
Spoiler:
Code: Select all
    function ListFiles($dir=NULL$recursive=true$object=true){
        if(
$dir==NULL){
            
$this->PushError("No location has been provided for scaning!");
            exit;
        }
        
$dir    =    realpath($dir);
        
#
        
$list    =    array();
        
#
        
$d  opendir($dir);
        while (
false !== ($filename readdir($d))) {
            if(
$filename!="." && $filename!=".."){
                switch(
$object){
                    case 
true:
                        
$list[$filename]=new FileInfo(realpath($dir."/".$filename), $recursive$object);
                        break;
                    default:
                        
$list[$filename]=$this->Info(realpath($dir."/".$filename), $recursive$object);
                        break;
                }
            }
        }
        
closedir($d);
        
#
        
ksort($list);
        
#
        
return $list;
    }


Good luck. Grin
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10


Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Pace » 02 Mar 2007, 02:00

I think this is somewhat right?

Spoiler:
Code: Select all
function ListFiles($dir NULL, $recursive true, $object true)
{
    if (
$dir == NULL)
    {
        
trigger_error('NO_SCANNING_LOCATION');
        
// this error ("No location has been provided for scaning!") must be defined in the common.php language file 
    
}

    
$dir realpath($dir);
    
$list = array();
    
$d = @opendir($dir);

    
while (false !== ($filename = readdir($d)))
    {
        if (
$filename != "." && $filename != "..")
        {
            switch (
$object)
            {
                
case true:
                    
$list[$filename] = new FileInfo(realpath($dir."/".$filename), $recursive, $object);
                break;

                default:
                    
$list[$filename] = $this->Info(realpath($dir."/".$filename), $recursive, $object);
                break;
            }
        }
    }

    
closedir($d);
    @
ksort($list);
    
return $list;
}
User avatar
Pace
Style Author
Style Author
 
Posts: 147
Joined: 30 Aug 2006, 02:56
Location: Culuria, Residio
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Highway of Life » 02 Mar 2007, 13:44

Not bad... Good
But there are still missing a few things.
I won?t go into detail, because I don?t want to give too many hints.
  • Correct capitalization
  • Correct variable and function names (concise but explanatory).
  • Correct use of quotations
  • Trigger error will just display NO_SCANNING_LOCATION instead of a language variable, but you are on the right track.
  • Correct spaces on combining variables (use of ?.? and spaces between the period and the variables)
  • Use an alternate NULL value than just "NULL"
  • See if you can use a better while loop
  • Take every opportunity to optimize that code. Smile -- (there are a few locations where it can be optimized)
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby Richard » 31 Mar 2007, 01:49

Okay heres mine;

Spoiler:
Code: Select all
<?php
echo "can you make beginners challenges even easier? :P"
die;
?>
Richard
Supporter
Supporter
 
Posts: 64
Joined: 24 Jul 2006, 15:34
Gender: Male

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby silencer0 » 05 Apr 2007, 15:27

hmm i don't know if mine's correct already but anywayz Tongue i'll post it lol
Hope it's a bit correct lol
Spoiler:
Code: Select all
    function ListFiles($dir NULL$recursive true$object true){
        if(isset(
$dir == NULL))                                                
                                                                      {
        { 
            
$this->PushError("No location has been provided for scaning!");
            exit;
        }
        
$dir realpath($dir);
        
#
        
$list = array();
        
#
        
$d  opendir($dir);
        while (
false !== ($filename readdir($d))) {
            if(isset(
$filename != "." && $filename != .. ))
                                                    }
                switch(
$object){
                    case 
true:
                        
$list[$filename] = new FileInfo(realpath($dir."/".$filename), 

$recursive$object);
                        break;
                    default:
                        
$list[$filename] = $this->Info(realpath($dir."/".$filename), 

$recursive$object);
                        break;
                }
            }
        }
        
closedir($d);
        
#
        
ksort($list);
        
#
        
return $list;
    }
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 Highway of Life » 05 Apr 2007, 20:14

Nea emm... no... that code would ?break?.

Review Pace?s rendition for tips.
His code would actually work, and is really good, but I?m hard on him to get it perfect. Grin
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby silencer0 » 06 Apr 2007, 02:43

actually now, i've worked on it lil more time.. so now i suppose it's right..just one thing i didn't know was the trigger error, got that one from pace...

Spoiler:
Code: Select all
    function ListFiles($dir = NULL, $recursive = true, $object = true)
{
        if(isset($dir == NULL))                                               
                                                                     
        {
        trigger_error('NO_SCANNING_LOCATION');
        // The error ("No location has been provided for scaning!") must be defined in the common.php language file

        }
        $dir = realpath($dir);
        $list = array();
        $d  = opendir($dir);

        while (false !== ($filename = readdir($d))) {
            if(isset($filename != "." && $filename != .. ))
                                                    }
                switch($object)
{
                    case 'true':
                        $list[$filename] = new fileinfo(realpath($dir."/".$filename), $recursive, $object);
                                     //I am doing something here.
                    break;

                    default:
                        $list[$filename] = $this->Info(realpath($dir."/".$filename), $recursive, $object);
                                    //I am doing something totally different here.
                    break;
}
            }
        }
        closedir($d);
        ksort($list);
        return $list;
}
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 » 06 Apr 2007, 02:53

Highway, mind if i post mine? Tongue
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 Highway of Life » 06 Apr 2007, 10:45

eviL3 wrote:Highway, mind if i post mine? Tongue

Please do! Yahoo

Silencer... much better!! Good

That could would still break, however... there is an error in there.
Also, be sure to pay attention to proper function AND variable naming.
All names should be lowercase, and functions should be descriptive, but concise.
Variables should be long enough to tell the programmer what they are used for, but short. (unless they are counters, such as $i++; etc)
Watch out! I might do a code wheelie!

User avatar
Highway of Life    
STG Jedi Master
STG Jedi Master
 
Posts: 10458
Joined: 08 May 2006, 05:23
Location: Beware of Programmers carrying screwdrivers
Gender: Male
phpBB Knowledge: 10

Re: [Challenge] Format ListFiles to phpBB3 Coding Guidelines

Postby eviL3 » 06 Apr 2007, 11:33

Here you are:

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;
}
Image
User avatar
eviL3    
MOD Author
MOD Author
 
Posts: 1002
Joined: 05 Nov 2006, 08:14
Location: Cooking in the MODs kitchen
Gender: Male

Next

Return to phpBB3 Challenges at phpBB Academy

Who is online

Users browsing this forum: No registered users and 2 guests

cron