Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm trying to figure out if there's a way I can simplify this code which is used to generate a robots.txt file with numerous rules. Different files/folders are separated in separate arrays because they're applied differently but in some cases they are applied the same. In these cases, rather than write out a separate foreach for each one, this is what I'm trying to simplify:

<?php

public function generateRobotsFile($rbfile, $smfile) {
    $tab = array();
    $tab['SemBot'] = array('Googlebot',"bingbot\nCrawl-delay: 5",'MSNBot');

    $tab['Lang'] = array('eb');
    $tab['Files'] = array('address.php','cart.php');
    $tab['Folder'] = array('classes','docs','themes');

    fwrite($writeFd, "\nUser-agent: *\n");
    foreach ($tab['Lang'] as $Lang) {
        fwrite($writeFd, 'Disallow: ' . __PS_BASE_URI__ . $Lang . "\n");
    }
    foreach ($tab['Folder'] as $Folder) {
        fwrite($writeFd, 'Disallow: ' . __PS_BASE_URI__ . $Folder . "\n");
    }

    foreach ($tab['SemBot'] as $SemBot) {
        fwrite($writeFd, "User-agent: ' . $SemBot . "\n");
        foreach ($tab['Files'] as $Files) {
            fwrite($writeFd, 'Disallow: ' . __PS_BASE_URI__ . $Files . "\n");
        }
        foreach ($tab['Folder'] as $Folder) {
            fwrite($writeFd, 'Disallow: ' . __PS_BASE_)URI__ . $Folder . "/\n");
        }
    }

The arrays must remain separated but in some cases they are looped through the same disallow rule so I'm reaching for a foreach array in array but haven't quite grasped it yet.

share|improve this question
    
There's a couple of syntax errors in this; "User-agent: ' uses the wrong quote types and a bracket in __PS_BASE_)URI__ –  MrLore May 26 at 3:01
add comment

1 Answer

up vote 0 down vote accepted

If number of lines is your primary concern, then really all we can do with this is merge your array creation lines, and use array_merge() to create temporary arrays which you can loop around, like so:

public function generateRobotsFile($rbfile, $smfile)
{
    $tab = array('SemBot' => array('Googlebot', "bingbot\nCrawl-delay: 5", 'MSNBot'),
                 'Lang'   => array('eb'),
                 'Files'  => array('address.php', 'cart.php'),
                 'Folder' => array('classes', 'docs', 'themes'));
    fwrite($writeFd, "\nUser-agent: *\n");
    foreach(array_merge($tab['Lang'], $tab['Folder']) as $disallow)
    {
        fwrite($writeFd, 'Disallow: ' . __PS_BASE_URI__ . $disallow . "\n");
    }
    foreach($tab['SemBot'] as $SemBot)
    {
        fwrite($writeFd, "User-agent: " . $SemBot . "\n");
        foreach(array_merge($tab['Files'], $tab['Folder']) as $disallow)
        {
            fwrite($writeFd, 'Disallow: ' . __PS_BASE_URI__ . $disallow . "\n");
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.